nomorehumor
nomorehumor

Reputation: 69

How to set segue from button made programmatically

I create a button by code:

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

But now I want to do a segue, when button is tapped. Usually I do it with @IBAction, but now I made this button not in IB, but by code. So, how to set segue?

Upvotes: 0

Views: 104

Answers (2)

hardikdevios
hardikdevios

Reputation: 1789

give your segue directly from current view controller to destination controller and give it identifier And in Action call with identifier like this

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

Upvotes: 2

In your Action: method (which you should probably rename to action:), you can just call performSegueWithIdentifier:sender: on the view controller that has the segue attached in the storyboard.

Upvotes: 2

Related Questions