Unwind segue with Navigation back button

后端 未结 4 876
孤街浪徒
孤街浪徒 2020-12-14 06:35

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.

相关标签:
4条回答
  • 2020-12-14 07:05

    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

    0 讨论(0)
  • 2020-12-14 07:06

    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.

    0 讨论(0)
  • 2020-12-14 07:07

    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)
    }
    
    0 讨论(0)
  • 2020-12-14 07:15

    @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];
    }
    
    0 讨论(0)
提交回复
热议问题