从另一个xib中的自定义视图(xib)调用操作

时间:2015-03-04 20:53:52

标签: ios swift storyboard xib

我的蓝图是这样的:

  • 我有UIViewController加载Xib(Main.xib)。
  • 此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)
    }
}

Info Info2 如果有人更感兴趣,我也会给出我所做的测试项目的链接: Link to a test project

0 个答案:

没有答案
相关问题