Unwind segue with Navigation back button

て烟熏妆下的殇ゞ 提交于 2019-11-27 12:47:14

问题


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


回答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




回答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];
}



回答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)
}



回答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.



来源:https://stackoverflow.com/questions/13551778/unwind-segue-with-navigation-back-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!