我的蓝图是这样的:
UIViewController
加载Xib(Main.xib)。 UIView
指向另一个自定义视图
xib(Header.xib)。如果我在Main.xib中创建一个按钮并向我的ViewController
添加动作(打印),那么它可以正常工作,因为我将Main.xib的FileOwner
设置为ViewController.swift。但是,在Header.xib
我还有另一个按钮,我也希望能够连接到我的ViewController
,但我收到了以下错误,尽管我还将文件所有者设置为ViewController
:
2015-03-04 21:49:11.035 Testing[25795:1041547] -[Testing.Header printing:]: unrecognized selector sent to instance 0x7f9de0d40dc0
2015-03-04 21:49:11.040 Testing[25795:1041547] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Testing.Header printing:]: unrecognized selector sent to instance 0x7f9de0d40dc0'
首先抛出调用堆栈:
有什么想法吗?
class ViewController: UIViewController {
override func loadView() {
view = NSBundle.mainBundle().loadNibNamed("Main", owner: self, options: nil).first as UIView
}
@IBAction func printing(sender: AnyObject) {
println("Testing action")
}
}
class Header: UIView {
@IBOutlet weak var view: UIView!
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
NSBundle.mainBundle().loadNibNamed("Header", owner: self, options: nil).first as UIView
self.addSubview(self.view)
}
}
如果有人更感兴趣,我也会给出我所做的测试项目的链接: Link to a test project