Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check which segue was used

I got two segue's which lead to the same viewController. There are 2 buttons which are connected to the same viewController using 2 segues. In that viewController I need to check which button was clicked. So actually I need to check which segue was used/preformed. How can I check this in the viewControllers class? I know there is the prepareForSegue method, but I cannot use this for my purpose because I need to put the prepareForSegue in the class where the 2 buttons are, and I don't want it there but I want it in the viewControllers class because I need to access and set some variables in that class.

like image 684
nonuma Avatar asked Apr 23 '13 10:04

nonuma


People also ask

What is present Modally segue?

Present Modally. This segue displays the view controller modally using the specified presentation and transition styles. The view controller that defines the appropriate presentation context handles the actual presentation.

What does unwind segue do?

What is an unwind segue and how does it work? An unwind segue lets you jump to any view controller further up your view controller hierarchy, destroying all view controllers preceding the destination. Before actually creating one, let's overview how an unwind segue works.

How do I connect to segue in Swift?

The first step is to find the “Storyboard Reference” item in our library and add it to the storyboard we want to segue from. You'll also want to add the Storyboard name and View Controller's Storyboard ID that you want to segue to in the Attributes Inspector of the Storyboard Reference.

What is segue destination?

A segue defines a transition between two view controllers in your app's storyboard file. The starting point of a segue is the button, table row, or gesture recognizer that initiates the segue. The end point of a segue is the view controller you want to display.


2 Answers

You need to set a variable of the second viewcontroller in the prepareforsegue method of first one. This is how it is done:

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:segueIdentifier1])
    {
        SecondViewController *secondVC = (SecondViewController *)segue.destinationViewController;
        if(sender.tag == ...) // You can of course use something other than tag to identify the button
        {
            secondVC.identifyingProperty = ...
        }
        else if(sender.tag == ...)
        {
            secondVC.identifyingProperty = ...
        }
    }
}

Then you can check that property in the second vc to understand how you came there. If you have created 2 segues in the storyboard for 2 buttons, then only segue identifier is enough to set the corresponding property value. Then code turns into this:

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:segueIdentifier1])
    {
        SecondViewController *secondVC = (SecondViewController *)segue.destinationViewController;
        secondVC.identifyingProperty = ...
    }
    else if([segue.identifier isEqualToString:segueIdentifier2])
    {
        SecondViewController *secondVC = (SecondViewController *)segue.destinationViewController;
        secondVC.identifyingProperty = ...
    }
}
like image 114
guenis Avatar answered Sep 28 '22 19:09

guenis


So firstly you need to set your segues identifier directly in storyborads or through your code using the performSegueWithIdentifier method. Independently the way you choosed, your view controller will fire the following method, so you need to override it to know which segue was sending the message, you do like this:

 -(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {    
        if ([segue.identifier isEqualToString:@"ButtonSegueIdentifierOne"]) {
            // button 1
        }
        if ([segue.identifier isEqualToString:@"ButtonSegueIdentifierTwo"]) {
            // button 2
        }
}
like image 24
Malloc Avatar answered Sep 28 '22 20:09

Malloc