narner
narner

Reputation: 3221

Unwind Segue with UIBarButtonItem

I've successfully implemented an unwind segue based on the answers to this question

What I can't get to work is using a BarButton Item to trigger the unwind, as opposed to a Button. The Button item triggers the segue correctly, but when I connect the BarButton Item to the method below, no transition occurs at all.

- (IBAction)unwindSegue: (UIStoryboardSegue *)segue 
{
    // Do Something    
}

I've used a BarButton item to trigger a push segue before, so I'm not sure why I can't also use it trigger an unwind segue. Anyone have any suggestions?

Upvotes: 0

Views: 1443

Answers (1)

Wizkid
Wizkid

Reputation: 1035

In IB you need to drag from VC2 down to the green Exit icon at the bottom of your VC2 and select the unwind segue from the list corresponding to VC1. This will wire VC2 to the unwind in VC1. To "trigger" this you can do something like this:

VC2

- (IBAction)buttonAction:(id)sender
{
    UIBarButtonItem *barButton = (UIBarButtonItem *)sender;

    // use a conditional like this if you want to use this action method for multiple buttons
    // Otherwise, just call performSegueWithIdentifier
    if (barButton == self.navigationItem.leftBarButtonItem) {
        [self performSegueWithIdentifier:@"myStoryboardIdForVC1" sender:self];
    }
}

VC1

- (IBAction)unwindToVC1:(UIStoryboardSegue *)segue
{
    // use condidtionals like this if you want to use the unwind from multiple sources
    if ([segue.identifier isEqualToString:@"mySegueIdfromVC2"]) {
       // do something
    }
    if ([segue.identifier isEqualToString:@"mySegueIdfromVC3"]) {
       // do something
    }
    if ([segue.identifier isEqualToString:@"mySegueIdfromVC4"]) {
       // do something
    }
}

Upvotes: 1

Related Questions