KML
KML

Reputation: 2322

How to use custom nib view in ViewController multiple times

I am trying to load a custom UIView from nib

CustomView.swift

import UIKit

@IBDesignable class CustomView: UIView {

    var view: UIView!

    override init(frame: CGRect) {
        super.init(frame: frame)

        xibSetup()
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        xibSetup()
    }


    func xibSetup() {
        view = loadViewFromNib()
        view.frame = bounds
        view.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight

        addSubview(view)
    }

    func loadViewFromNib() -> UIView {
        var nibName:String = "CustomView"
        var bundle = NSBundle(forClass: self.dynamicType)
        var nib = UINib(nibName: nibName, bundle: bundle)

        var view = nib.instantiateWithOwner(self, options: nil)[0] as UIView
        return view
    }

}

CustomView.xib

File's Owner is the CustomView class.

In StoryBoard

I have a UIViewController (Scroll View Controller) with a UIView with the custom class: CustomView. The xib @IBDesignable propagates through and all is well.

Now the problem starts. I am trying to use the customView multiple times (just like cells in a tableView) in the ScrollViewController, just as I do with viewOne

ScrollViewController.swift

import UIKit

class ScrollViewController: UIViewController {

    let scrollView = UIScrollView()


    override func viewDidLoad() {
        super.viewDidLoad()

        let height = CGFloat(200.0)

        self.scrollView.frame = self.view.bounds
        self.scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, height*3)
        self.view.addSubview(self.scrollView)


        var y = CGFloat(0.0)

        for i in 0..<2 {

            //Adding viewOne
            let viewOne = self.createViewOne(i)
            viewOne.frame = CGRectMake(0, y, 320, 200)
            self.scrollView.addSubview(viewOne)

            //Adding CustomView
            let customView = self.createCustomView(i)
            customView.frame = CGRectMake(0, y, 320, 200)
            self.scrollView.addSubview(customView)

            y += height
        }

    }


    func createViewOne(index: Int) -> UIView {
        let viewOne = UIView()

        if index == 0{
            viewOne.backgroundColor = UIColor.greenColor()
        }

        if index == 1{
            viewOne.backgroundColor = UIColor.redColor()
        }

        return viewOne
    }


    func createCustomView(index: Int) -> UIView {
        let customView: CustomView = NSBundle.mainBundle().loadNibNamed("CustomView", owner: self, options: nil)[0] as CustomView  --> Code breaks here

        if index == 0{
            customView.backgroundColor = UIColor.greenColor()
        }

        if index == 1{
            customView.backgroundColor = UIColor.redColor()
        }

        return customView
    }


}

Question the code breaks at the line below and console output is unhelpful (lldb)

let customView: CustomView = NSBundle.mainBundle().loadNibNamed("CustomView", owner: self, options: nil)[0] as CustomView

I have also tried to instantiate with this code:

customView = CustomView.loadViewFromNib()

but then I get error "Missing argument for parameter #1 in call"

1) How can I load a custom UIView from nib multiple times in ViewController. 2) How can I change the UI Elements contained within the view, such as UIImageView, UILabels etc. E.g. how can I set the title like customView.title.text = "Fido"

Any help would be very much appreciated ! Thank You.

Upvotes: 1

Views: 3414

Answers (1)

Wain
Wain

Reputation: 119031

The whole point of your view class is that it loads the nib and adds the created view as a subview. It is also defined as the owner of the nib.

So, when your view controller tries to load the nib with itself as the owner it has all the outlet setters called on it, but it doesn't implement them, so it breaks.

To fix, you should be alloc and initing yhe view, not loading it from the nib explicitly in the view controller.

Replace

let customView: CustomView = NSBundle.mainBundle().loadNibNamed("CustomView", owner: self, options: nil)[0] as CustomView

with

let customView = CustomView()

Upvotes: 3

Related Questions