Segue and Button Programmatically Swift

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 programmatically perform segue, or present view, when button pressed in swift3

segue is Interface Builder lingo. In iOS, you can only display a view controller three ways: push, present, show. Programmatically, you wouldn't call it segue. First, add a target to your button:

registerButton.addTarget(self, action: #selector(registerButtonAction), for: .touchUpInside)

Create the action method:

@objc func registerButtonAction() {

And if you want to push to a view controller (using a navigation controller), push to it from inside the action method:

@objc func registerButtonAction() {
let destination = SomeViewController()
navigationController?.pushViewController(destination, animated: true)

If you want to present it (modally):

@objc func registerButtonAction() {
let destination = SomeViewController()
destination.transitioningDelegate = SomePresentationAnimationVendor() // for custom presentations
destination.modalPresentationStyle = .custom // for custom
present(destination, animated: true, completion: nil)

Creating a segue programmatically without a storyboard

I assume you meant a ViewController by the word page. So initialize your ViewController inside your func regSegue(){...} and present it using present(_:animated:completion:) method.


func regSegue(){
let page = PageViewController()
present(page, animated: true, completion: nil)

How do I stop a segue from going through programmatically in Swift?

First , there is no "opposite" to performSegue(withIdentifier: String, sender: Any?).

But the issue is not about this. I think you wired the segue from the login button and gave it an identifier. If you wire a segue from a button directly the button is always going to execute that segue. Doing some operations in the button's action does not effect.

You need to wire a segue from FirstVc to SecondVc (not from the button) and then give the segue an identifier. Then, from the button's action you can check if there is no error and call performSegue(withIdentifier: String, sender:) passing your segue's identifier.

perform segue from programmatically button

  • Have you established the segue? In IB ⌃-drag from the the source controller (yellow icon) to the destination view controller)?
  • Have you specified the identifier? Select the segue, press ⌥⌘4 and type moveToNext in the Identifier field.

Segue to a ViewController from a UIButton in Swift

You need to embed each vc in the tab inside a navigationController , then use this to navigate to a nested vc

@objc func clickStart(_ sender: UIButton) { 
let vc = self.storyboard!.instantiateViewController(withIdentifier: "VCID") // set id for the destination vc

And this to navigate to another vc inside the tab

self.tabBarController?.selectedIndex = 1 // 1 is the second tab

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?

Make Segue programmatically in Swift

You can do it like proposed in this answer:

Furthermore I am providing you the code from the linked answer rewritten in Swift because the answer in the link was originally written in Objective-C.

let vc = UIStoryboard(name:"Main", bundle:nil).instantiateViewController(withIdentifier: "identifier") as! SecondViewController

vc.resultsArray = self.resultsArray


Since this answer draws some attention I thought I provide you with another more failsafe way. In the above answer the application will crash if the ViewController with "identifier" is not of type SecondViewController. In Swift you can prevent this crash by using optional binding:

guard let vc = UIStoryboard(name:"Main", bundle:nil).instantiateViewControllerWithIdentifier("identifier") as? SecondViewController else {
print("Could not instantiate view controller with identifier of type SecondViewController")

vc.resultsArray = self.resultsArray
self.navigationController?.pushViewController(vc, animated:true)

This way the ViewController is pushed if it is of type SecondViewController. If can not be casted to SecondViewController a message is printed and the application remains on the current ViewController.

Related Topics

Leave a reply