简体   繁体   中英

Reference presenting view controller from modal (objective-c)

I have a navigation stack consisting of:

UINavigationViewController
 -> WelcomeViewController
 -> RoleViewController

I would like to add a modal viewcontroller in between Welcome and Role. I add a ModalViewController via storyboard and assign a segue (Present Modally) to the ModalViewController from a button in WelcomeViewController. It's all good and the modal shows up fine.

Now that I'm inside the ModalViewController I would like to dismiss the modal and perform the original segue from Welcome to Role. I did some searching on StackOverflow and came over different takes on how to do this. The most logical way was set out here: Dismissviewcontroller and perform segue

UIViewController *parentController = self.presentingViewController;
[self dismissViewControllerAnimated:YES completion:^(void){
    [parentController performSegueWithIdentifier:@"segue_gotoRole" sender:self];
}];    

The modal is dismissed but the segue is not executed and the app crashes:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: 'Receiver (<NavigationViewController: 0x78b96b80>) has no segue
with identifier 'segueToRole''

So the self.presentingViewController targets the UINavigationViewController and not the view controller WelcomeViewController which is actually presenting the modal. To succeed I need to find a way to get the presenting view controller which is defined in the storyboard as the segue originator from within the modal .

I've tried a lot of different combinations, eg self.parentViewController, self.presentationViewController, I've even tried to loop through the navigation stack to single out the WelcomeViewController and target it specifically as the object to perform the segue, but nothing seems to work.

Please help me. :)

如果是我,我会写一个delegate protocol在这里Modal VC ,你提出并WelcomeViewControllerdelegate -一旦它被驳回,则WelcomeViewController可以接管和原因请看RoleViewController

What I did is to send the presenter reference to the presented instance.

This can be implemented by setting up a property of presenting class reference in the presented class.

For example,

@property (strong, nonatomic) PresentingClass *presenter;

.

Then, in the presenting class implementation

presentedClass.presenter = self;
[self presentViewController:presentedClass animated:YES completion:nil];

Finally, in the presented implementation, simply call

if(presenter){
  [presenter foo];
}

Could help. Let me know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM