Swift: Segue on Button Click

Swift: Make button trigger segue to new scene

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

swift: segue on button click

It's very simple.

Follow these steps to create segue from your tableview cell button (click).

  1. Open your storyboard layout (view controller)
  2. Add new (destination) view controller.
  3. Select your button.
  4. Press & hold control ctrl button from keyboard and drag mouse cursor from your button to new (destination) view controller.
  5. Now add following code to your source view controller file (source code)

-

override func performSegue(withIdentifier identifier: String, sender: Any?) {
print("segue - \(identifier)")

if let destinationViewController = segue.destination as? <YourDestinationViewController> {
if let button = sender as? UIButton {
secondViewController.<buttonIndex> = button.tag
// Note: add/define var buttonIndex: Int = 0 in <YourDestinationViewController> and print there in viewDidLoad.
}

}
}


Sample Image

Another way to handle the same.

How to perform different segue based on condition from a button click in swift?

I think, You are trying to connect the button to perform segue with multiple ViewController Right?? which is not possible

Instead that you have to connect segue between View Controllers

Adding a segue between two viewControllers:

From the Interface Builder, ctrl + drag between the two viewControllers that you want to link. You should see:

Sample Image

And now based on you condition you should perform segue with identifiers like below:

@IBAction func NextViewController(_ sender: UIButton) {
if launchedBefore {
/*Use the Identifier you given in story Board*/
self.performSegue(withIdentifier: "otherVC", sender: self)

} else {
/*Use the Identifier you given in story Board*/
self.performSegue(withIdentifier: "register", sender: self))
UserDefaults.standard.set(true, forKey: "launchedBefore")
}
}

For more Descriptive answer about segue see the answer How to connect two view controllers to one button in storyboard?

Segue and Button programmatically swift

Create seuge

Create Seuge

Assign identifier

Sample Image

and your button target

 @IBAction func button_clicked(_ sender: UIButton) {
self.performSegue(withIdentifier: "segueToNext", sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segueToNext" {
if let destination = segue.destination as? Modo1ViewController {
destination.nomb = nombres // you can pass value to destination view controller

// destination.nomb = arrayNombers[(sender as! UIButton).tag] // Using button Tag
}
}
}

How to visualize if a button is set to trigger segue to another View Controller?

In Main.storyboard, click on the viewcontroller that you are interested in inspecting. In the top right there is a right arrow that will show you all the segue relationships and outlets

I attached a picture.

Button Activated Segue Not Being Called

I have two segues triggered by buttons

I'm going to suggest that in fact no, you don't. One of the segues is perhaps triggered by a button; that's the one that works as you expect — the segue fires when the button is tapped. But the other segue, though it exists, does not emanate from the other button; it emanates from the cell or from the view controller as a whole.

The way to check this is to select the button and display the Connections Inspector:

Sample Image

If you don't see that — an "action" triggered segue associated with the button — tapping the button won't fire the segue.

Push Action Segue from button to UIViewController

From error logs it looks like you have not embedded UINavigationController as initial controller in you storyboard.

So first set UINavigationController as initial controller.
Because push and pop operation only performed when there is Navigation stack available.

  1. Select first ViewController in Storyboard as per screen shot.
  2. Then Editor -> EmbedIn -> NavigationController

To know how Navigation Stack works, go through this link.

Sample Image

Once you set Navigation as InitialViewController, then set your first viewcontroller you want to show first as rootViewController of that navigation controller.

Sample Image

Then you can drag push from button to secondViewController to push from firstViewController to secondViewController.

Sample Image

As you can read in screenshot that Push(Deprecated), so you have to use Show relationship. Show also perform same operation as Push.

Update :

As per your question update, you are facing that you are not able to show other Action Segue. Follow below steps to make it available.

  1. Select your Navigation Controller in storyboard.
  2. Enable Use Trait Variations option under Interface Builder Document on right side panel.

Sample Image

For more details check this thread.

Swift prepare for Segue: How to know If a button or a table row initiated Segue

Just pass the index path as sender

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

performSegue(withIdentifier: "toMap", sender: indexPath)

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

if segue.identifier == "toMap"
{
let mapViewController = segue.destination as! ViewController

if let buttonItem = sender as? UIBarButtonItem, buttomItem.tag == 1 {
mapViewController.activePlaceRow = -2
}
else if let indexPath = sender as? IndexPath {
mapViewController.activePlaceRow = indexPath.row
}
}
}


Related Topics



Leave a reply



Submit