Theo
Theo

Reputation: 465

iOS 7 - Identify segue that called view to unwind correctly

I have a UIViewController that can be presented by two different other views. It's a search form that act exactly the same for both views.

Let's say View1 and View2 can perform segues that present an instance of SearchView. How could I know which view presented SearchView so that I can perform an unwind segue that is linked to the corresponding action?

As both View1 and View2 are TabViews, I don't want to use a navigationController in each of them as I find it a little bit of overpower for a simple task.

In this example:

View1.m

-(IBAction)didClickButton:
{
    [self performSegueWithIdentifier:@"searchSegue1"];
}

View2.m

-(IBAction)didClickButton:
{
    [self performSegueWithIdentifier:@"searchSegue2"];
}

SearchView.m

// Here is where I should know which segue to call
if (***ConditionToSegue1***)
    [self performSegueWithIdentifier:@"unwindToSegue1"];

else if (***ConditionToSegue2***)
    [self performSegueWithIdentifier:@"unwindToSegue2"];

What is this condition? Is the best way to do it to just set a property of SearchView in the prepareForSegue methods that tells me what view that originated the call? Please keep in mind this I do no wish to use a NavigationController.

Best regards

Upvotes: 0

Views: 492

Answers (4)

Theo
Theo

Reputation: 465

I've found out that there is an easy answer to this. Apparently, if I create an unwind action on both view controllers named just the same, I can create just one unwind segue for both of them as well.

If I perform this segue on the SearchView it calls the correct action depending on who presented it.

This seems to work for now, I don't really know why or how it happens. Would appreciate if anyone could explain.

UPDATE: Nope, it doesn't work as expected. Although the correct view is presented both actions are called, resulting in unwanted behavior.

Upvotes: 0

Joe McBride
Joe McBride

Reputation: 3777

In your situation it may be as simple as [self dismissViewControllerAnimated: YES completion: nil];

Check out this blog post near the end that's titled Unwind Segues.

http://chrisrisner.com/Unwinding-with-iOS-and-Storyboards

You can wire up an action to the Exit component on the Storyboard which will do the unwinding for you.

-(IBAction)reset:(UIStoryboardSegue *)segue {
    //do stuff
}

Upvotes: 0

user3182143
user3182143

Reputation: 9599

First of all you should know whether you are using storyboard or programatically creating storyboard for PERFORMING SEGUE.

    1.If you directly connect the segue through the click button(one view controller to another view controller),you don't need to write your above coding.Just it is enough to give segue name. 
            click->SHOW THE ATTRIBUTES OF RIGHT SIDE UTILITY AREA.
                 ->NOW YOU CAN SEE THE STORYBOARD SEGUE WITH-IDENTIFIER,STYLE 
                 ->IN IDENTIFIER TEXTFIELD JUST GIVE YOUR SEGUE NAME(WHATEVER YOU WANT)
                   (IN STYLE-PUSH IS DEFAULT IF YOU CONNECT THE SEGUE AS A PUSH)

Upvotes: 0

Pranav
Pranav

Reputation: 680

In your SearchView.h add the line @property (nonatomic) int tag.

Then in View1.m add the following method:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"searchSegue1"])
        [segue.destinationViewController setValue:1 forKey:@"tag"];
}

Do the same thing in View2.m but replace 1 with 2. Then change your if statement to:

if (self.tag == 1)
    [self performSegueWithIdentifier:@"unwindToSegue1"];
else if (self.tag == 2)
    [self performSegueWithIdentifier:@"unwindToSegue2"];

Upvotes: 3

Related Questions