Unwind segue with Navigation back button

22,911

Solution 1

In the end I didn't need to unwind the segue since I could still get a reference to the parent controller methods by following the navigation controller.

I was able to get a reference by doing the following in the - (void)viewWillDisappear:(BOOL)animated method of the child controller

NSInteger currentVCIndex = [self.navigationController.viewControllers indexOfObject:self.navigationController.topViewController];

FirstViewController *parent = (FirstViewController *)[self.navigationController.viewControllers objectAtIndex:currentVCIndex];

parent.barcodeType = indexPath.row;

which passed the settings variable back to the original controller perfectly.

I also added an import reference to the parent controller at the top of the childcontroller

Solution 2

@Alan is on the right track. Just add a test to prevent the segue from firing when not going back to the first view controller. Something like this in viewWillDisappear will actually allow you to perform the unwind segue for a back button. You will need to create a manual unwind segue by dragging from the class down to the exit icon. Be sure to name it after you create it.

UIViewController *vc = self.navigationController.topViewController;
if ([FirstViewController class] == [vc class])
{
    [self performSegueWithIdentifier:@"unwindSegue" sender:self];
}

Solution 3

I'd like to add a Swift solution that follows @Chuck H suggestion above:

let rootVC = self.navigationController!.topViewController
if rootVC.isKindOfClass(TheNameOfYourViewController) {
    performSegueWithIdentifier("yourNamedSegueIdentifier", sender: self)
}

Solution 4

An alternative you might have not considered is to make the parent a delegate of the child and use delegate methods in the child to update the parent.

That way you would bypass the need to add to the navigation unwind, because the parent is already up to date the moment you hit back.

Share:
22,911
Alan
Author by

Alan

Updated on December 17, 2020

Comments

  • Alan
    Alan over 3 years

    I have watched the apple demo video on storyboard implementation with the unwind segues section and seen the demo using custom buttons on the main view. That works fine.

    I have also seen a good example of the same thing here which works also. http://www.techotopia.com/index.php/Using_Xcode_Storyboarding_%28iOS_6%29#Unwinding_Storyboard_Segues

    However, I wish to have a normal push segue passing from parent (with main form data) to child (with advanced settings options) and then return to the parent view controller using the back button in the navigation bar rather than with a custom button on the view as shown in the demo. The parent controller will then save everything to an API server when a further save button on the parent controller is pressed.

    I dont seem to be able to override the navigation back button to point it down to my unwind segue action and since the back button doesnt appear on the storyboard, I cant drag the green Exit button to it

    I tried this in viewDidLoad to overrride the action, but it didnt work [self.navigationItem.backBarButtonItem setAction:@selector(unwindSegueAction:)];

    Is it possible to unwind a push segue with the back button?

    The best idea I had so far was to override the back button from the viewDidLoad method, but that removes the angled back button style and also seems like a rough hack to a simple problem

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStyleBordered target:self action:@selector(unwindSegue:)];
    

    I know there are answers for using protocol and delegates, but I am interested in using the Xcode4.5 unwind segue method

  • Daniel
    Daniel almost 10 years
    Good idea, could be problematic if your view controller can also go forward. Or if you have another modal that can be triggered above this one.
  • Raghavendra
    Raghavendra almost 10 years
    I have same problem, I am using Swift. Can you provide the above solution in Swift? Thanks!