UIButton addtarget到不同的视图控制器

时间:2016-01-08 22:16:58

标签: ios swift uibutton

我在另一个button上向view添加了ViewController。我需要目标选择器指向该视图控制器:

//target is self which works only works where I created the button
testBTN.addTarget(self, action: "test:", forControlEvents: UIControlEvents.TouchUpInside)

//otherVC is being instantiated as a new view controller so it doesn't work
testbtn.addTarget(otherVC(), action: "test:", forControlEvents: UIControlEvents.TouchUpInside)

如何让目标指向已存在的ViewController(otherVC)?

1 个答案:

答案 0 :(得分:2)

想通了

let otherVC = self.storyboard!.instantiateViewControllerWithIdentifier("otherIdentifier")
testbtn.addTarget(otherVC, action: "test:", forControlEvents: UIControlEvents.TouchUpInside)