Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: Make button trigger segue to new scene

Tags:

Alright, so I have a TableView scene and I'm going to put a button in each of the cells and I need each cell to, when clicked, segue to its own ViewController scene. In other words, I need to know how to connect a button to a scene (The only button I have right now is "milk")

I know how to create an IBAction linked to a button, but what would I put in the IBAction?

I'm a beginner so I need a step-by-step explanation here. I've included a picture of my storyboard. I haven't written any code yet. enter image description here

like image 362
blue Avatar asked Sep 28 '14 19:09

blue


People also ask

How do you segue on a button click in Swift?

If you want to have a button trigger the segue transition, the easiest thing to do is Control+Click from the button to the view controller and choose a Segue option (like push).

How do you trigger a segue?

First, select a segue in your storyboard, then go to the attributes inspector and give it a name such as “showDetail”. Technically the sender parameter is whatever triggered the segue, but you can put whatever you want in there.

How do I create a segue between view controllers in Swift?

To create a segue between view controllers in the same storyboard file, Control-click an appropriate element in the first view controller and drag to the target view controller. The starting point of a segue must be a view or object with a defined action, such as a control, bar button item, or gesture recognizer.


1 Answers

If you want to have a button trigger the segue transition, the easiest thing to do is Control+Click from the button to the view controller and choose a Segue option (like push). This will wire it up in IB for you.

If you want to write the code to do this yourself manually, you can do so by naming the segue (there's an identifier option which you can set once you've created it - you still need to create the segue in IB before you can do it) and then you can trigger it with this code:

V2

@IBAction func about(sender: AnyObject) {     performSegueWithIdentifier("about", sender: sender) } 

V3

@IBAction func about(_ sender: Any) {     performSegue(withIdentifier: "about", sender: sender) } 
like image 56
AlBlue Avatar answered Sep 20 '22 19:09

AlBlue