简体   繁体   中英

UIButton addtarget to a different view controller

I'm adding a button to a view on a different ViewController . I need the target selector to point to that view controller:

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

How can I get the target to point to an already existing ViewController (otherVC)?

figured it out

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM