Search code examples
iosswiftuiviewcontrolleruibuttonsegue

Creating multiple segues from one UIButton to the same view controller or another


i'm fairly new to swift, so please bear with me.

right now my problem can be broken down to this:

I have one Test View Controller that can display my 'Test' object by showing the description and the title. I also have an array of Test objects. In the top right hand corner, there is a 'skip this test' button, and if the user clicks on it, the viewcontroller will segue to itself, but change the Test object that is being displayed, e.g. its just basically looping through an array of Tests. I have changed the prepare for segue methods accordingly to push the data through the view controllers.

However, I want to be able to move to a completely different ViewController (lets just call it FinalViewController) if I have reached the last Test in my Test array.

Now this is the part that I can't fix.

I would like to create a segue directly from the 'skip test' button, only that it segues to a different view controller, depending on a certain condition. However, as i tried creating a segue in IB by right clicking on the Button and pulling it to the FinalViewController, it erased my previous segue I had for that button.

Does anybody know if there is a fix for this problem? thank you!!!!


Solution

  • However, as i tried creating a segue in IB by right clicking on the Button and pulling it to the FinalViewController, it erased my previous segue I had for that button

    Actually, you don't want to do that, instead, you should drag from the controller itself, not from the button because if your segue has been created based on a button, tapping on it should always perform it and this is NOT what you want.

    After creating a segue from the ViewController, you can -programmatically- call performSegue method and handle what data should be passed to the next viewController by implementing prepareForSegue method.

    For more information, check my answer to know how to create multiple segues with identifiers and work with them.


    EDIT:

    If you want to push to the same current ViewController, performSegue method would not be the optimal solution. You should instead push/present to same ViewController it self:

    class ViewController: UIViewController {
        var myString: String?
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // here we go
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let sameViewController = storyboard.instantiateViewControllerWithIdentifier("ViewControllerStoryboardID") as! ViewController
            sameViewController.myString = "the value of the new myString"
            navigationController?.pushViewController(sameViewController, animated: true)
    
            print(myString)
        }
    }
    

    Probably, you don't want to implement it in viewDidLoad, it's just for demonstration purpose.

    Make sure that the ViewController has a storyboard ID (in code snippet, I assume it is "ViewControllerStoryboardID"):

    enter image description here