如何通过编程方式从按钮中设置segue



我创建了一个按钮:

var button   = UIButton.buttonWithType(UIButtonType.System) as! UIButton
button.frame = CGRectMake(100, 100, 100, 50)
button.setTitle("Button", forState: UIControlState.Normal)
button.addTarget(self, action: "Action:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)`

现在我想做一个segue,当button被点击时。通常我用@IBAction来做,但现在我不用IB来做这个按钮,而是用代码。如何设置segue呢?

在你的Action:方法中(你可能应该将其重命名为action:),你可以在storyboard中附加了segue的视图控制器上调用performSegueWithIdentifier:sender:

直接给你的segue从当前视图控制器目标控制器并给它标识符在Action调用中使用标识符像这样

self.performSegueWithIdentifier("your identifier", sender: self)

最新更新