Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unwind segue with Navigation back button

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

like image 463
Alan Avatar asked Nov 25 '12 14:11

Alan


People also ask

How do you unwind segue programmatically?

You can perform an unwind segue programmatically by 1. creating a manual segue (ctrl-drag from File's Owner to Exit), 2. giving it a name in IB, then 3. doing a normal -performSegueWithIdentifier:sender: in your code.

How do you make unwind segue in Swift?

Connect a triggering object to the exit control In your storyboard, create an unwind segue by right-clicking a triggering object and dragging to the Exit control at the top of your view controller's scene.

How do you segue back in Swift?

Set up the project Create three new view controllers in the Storyboard (I'm going to call them View Controller A, B, and C.) Embed them in a navigation controller (Editor → Embed In → Navigation Controller) Create a show segue from View Controller A to B and from B to C. Give each a unique identifier.


2 Answers

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

like image 146
Alan Avatar answered Sep 26 '22 19:09

Alan


@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]; } 
like image 37
Chuck H Avatar answered Sep 25 '22 19:09

Chuck H