prepareForSegue is not getting called when I click on a button without using performSegueWithIdentifier

30,660

Solution 1

Well, as it turns out, my view controller where button calls the modal view did not have the right class where prepareForSegue is implemented. It was the default UIViewController instead of my custom class.

The way I figured it out was by putting a break point in viewDidLoad and even that was not breaking and thus I suspected that in the storyboard I did not have the right class associated with the view where the button is implemented.

Solution 2

For others with this problem, if you are now using Swift 3 having the following function will not throw errors as it is the correct syntax but it will not work because it is the Swift 2 function:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // code
}

You must update to Swift 3 "prepare" function for it to work for segues:

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

Solution 3

When hooking up an automatic segue for a table view, there is, in addition to Amro's answer (not assigning your corresponding subclass), another two cases where prepareForSegue might not be called. Ensure you've:

  1. hooked up the segue from the table view prototype cell, not the table view controller.

  2. used a segue from under the "Selection Segue" group in the segue connection pop-up menu, not one under "Accessory Action".

Hooking up automatic segues

[Click image to enlarge]

Solution 4

Whether its an Modal or Push Segue below code will always be called

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"Create Label"]) {
        SignUpViewController *asker = segue.destinationViewController;
    }
}

Solution 5

In my case, it ocured because my controller was extending another controller (Eureka Form View Controller = FormViewController) witch has implemented the performSegue function like this:

open override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // code
}

My function was implemented like this:

func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // code
}

To solve this, i just added override before:

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

Voila!

Share:
30,660
Amro Younes
Author by

Amro Younes

Updated on September 06, 2020

Comments

  • Amro Younes
    Amro Younes over 3 years

    Based on the Stanford iOS course I am playing with modal view controllers. In the demo they have a button that would launch a modal view and when it is clicked the function prepareForSegue is called. I mimicked the code and implementation into my project with the only difference is that my demo is on an iPhone storyboard and theirs is on the iPad.

    I noticed that while my modal view controller is coming up, it does not call prepareForSegue prior to that. I searched the Stanford project to see where they may register any segue behavior before prepareForSegue is called but there is no evidence. Can anyone shed some light on this. I searched stack overflow and all I found were that users were missing the call implementation of performSegueWithIdentifier. However, in the Stanford demo they never do that.

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if ([segue.identifier hasPrefix:@"Create Label"]) {
            AskerViewController *asker = (AskerViewController *)segue.destinationViewController;
            asker.question = @"What do you want your label to say?";
            asker.answer = @"Label Text";
            asker.delegate = self;
        }
    
    }
    

    Here is an example of there storyboard: enter image description here

    Here is an example of my storyboard: enter image description here

    In the debugger when I stop in the Stanford Demo code the call stack shows that the storyboard is performing a segue action, what do I need to configure in my storyboard to achieve the same result? enter image description here