如何在 ViewController 中多次使用自定义笔尖视图

How to use custom nib view in ViewController multiple times

我正在尝试加载自定义 UI来自 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

文件的所有者是 CustomView class。

In StoryBoard

我有一个 UIViewController(滚动视图控制器)和一个带有自定义 class 的 UIView:CustomView。 xib @IBDesignable 传播通过,一切正常。

问题来了。我试图在 ScrollViewController 中多次使用 customView(就像 tableView 中的单元格一样),就像我对 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
    }


}

问题 代码在下面的行中断并且控制台输出没有帮助 (lldb)

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

我也试过用这段代码实例化:

customView = CustomView.loadViewFromNib()

但是我得到错误 "Missing argument for parameter #1 in call"

1) 如何在 ViewController 中多次从 nib 加载自定义 UIView。 2) 如何更改视图中包含的 UI 元素,例如 UIImageView、UILabels 等。我怎样才能将标题设置为 customView.title.text = "Fido"

非常感谢任何帮助!谢谢。

您的观点 class 的全部观点是它加载笔尖并将创建的视图添加为子视图。也定义为nib的拥有者。

因此,当您的视图控制器尝试将自己作为所有者加载 nib 时,它调用了所有 outlet 设置器,但它没有实现它们,所以它中断了。

要修复,您应该分配并初始化视图,而不是在视图控制器中显式地从笔尖加载它。

替换

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

let customView = CustomView()