How to use custom nib view in ViewController multiple times

纵然是瞬间 提交于 2019-12-11 06:28:51

问题


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.


回答1:


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()


来源:https://stackoverflow.com/questions/28551404/how-to-use-custom-nib-view-in-viewcontroller-multiple-times

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!