Tag Archives: Storyboard

Summary of IOS storyboard unwind segues

When using storyboard development, we often add a button on a scene, drag the button to the page we want to associate with, and finally choose push to jump. So scene_A and scene_B have a “sequence” jump. But sometimes, you want to trigger an action from scene_B to jump back to scene_A. If you do it the same way you did it, you’re going to have a problem. Because the scene_a that jumps back is not the scene_a that it was. So we’re going to have to use the unwind segue to our storyboard. Write a little summary of the demo:
Create the storyboard shown in the figure below. RedViewController pushes to YellowViewController, YellowViewController pushes to BlueViewController, and BlueViewController modal to GreenViewController. You can return RedViewController from YellowViewController and YellowViewController and RedViewController from BlueViewController. Of course, you can also go back to the BlueViewController from GreenViewController.

Back to the code. Since storyboard is used, the jump of push and model saves writing code, similarly, the jump of “return” also saves code.
RedViewController. M

– (IBAction)unwindSegueToRedViewController:(UIStoryboardSegue *)segue {
    
}
It’s important to note that the return value of this method must be an ibaction, and the argument must be a uistoryboardsegue. As for why, the following code will show.
Go back to your storyboard file and observe that the RedViewController scene has a green button below it (not because you wrote the code above, but because it’s always there).

Next, right click the exit button (green button), will appear just write method unwindseguetoredviewcontroller:

Select this method and drag and drop to the Back RedVC button on the YellowViewController.

After letting go, an Action prompt appears near Back RedVC and is selected. In this way, when you click the Back Red VC button on the YellowViewController, it will jump back to the Red View Controller. Let me compile it. There’s no problem with insurance. Run the program.
Instructions required:
1. If you like from yellowviewcontroller returns to redviewcontroller, then unwind segue associated methods must be stated in redviewcontroller again, that is the example of – (inaction) unwindtoredviewcontroller (segue uistoryboardsegue *); Note the parameters and return points, and the method name is arbitrary.
2. Right click on the exit button of redviewcontroller (green button) to show the method that unwind segue can associate with, and then go to the button of yellowviewcontroller.

Similarly, the BlueViewController can be returned to the Implementation of The YellowViewController.
In YellowViewoController. M

– (IBAction)unwindToYellowViewController:(UIStoryboardSegue *)segue {
    
}
Right-click the Exit button of The YellowViewController to associate BlueviewControler’s Back YellowVC button.
Also need to realize from the immediate return to redviewcontroller blueviewcontroller, selected redviewcontroller exit button, select the button on the blueviewcontroller unwindtoredviewcontroller associated.
Compile it, no problem, run it, get a feel for it.
Either the YellowViewController or the BlueViewController can be returned to the RedViewController, at which point you need to make a judgment to see where the return is coming from.

- (IBAction)unwindSegueToRedViewController:(UIStoryboardSegue *)segue {
    
    UIViewController *sourceViewController = segue.sourceViewController;
    
    if ([sourceViewController isKindOfClass:[YellowViewController class]]) {
        NSLog(@"from yellow vc");
    }
    else if ([sourceViewController isKindOfClass:[BlueViewController class]]) {
        NSLog(@"from blue vc");
    }
}

And that’s why the argument to a method is a segue, where you get the source View Controller.

Now when you try push, the same thing with modal, you present the GreenViewController from the BlueviewController in modal mode.
To return, you need to write the method associated with the unwind segue in the BlueviewController:

– (IBAction)unwindToBlueViewController:(UIStoryboardSegue *)segue {
    
}
Associated unwindtoblueviewcontroller methods on the corresponding button.
At this point, I’m done with “back” in my storyboard, and I spend most of my time dragging and dropping controls without writing the relevant pop dismiss method at all. That’s one of the strengths of storyboard.
Is it possible to implement it in code?You can.
Select GreenViewController Scene in your storyboard, and on the left you can see the representation of the unwind segue, kind of like the DNA sequence yes, once you select it, the only representation that defines it is the greenUnwind


In greenviewcontroller. M
Method associated with back Code button:

- (IBAction)backCodeBtnTapped:(id)sender {

    [self performSegueWithIdentifier:@"greenUnwind" sender:self];
}

Compile, there are no errors, the program.

Program all code.