简体   繁体   中英

Unable to dismiss UIViewController from UITabBarController

I have an iOS application which at the root has a UITabBarController (which has three tabs).

In the first tab in viewDidAppear: I have the following to present RegisterViewController if the user has not yet registered:

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
RegisterViewController *registerViewController = [mainStoryboard instantiateViewControllerWithIdentifier:@"RegisterView"];
[registerViewController setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentViewController:registerViewController animated:NO completion:nil];

When the user taps on a "Register" button in RegisterViewController it presents QuestionnaireViewController once the HTTP response has been received.

When the taps on a "Submit" button in QuestionnaireViewController I am able to dismiss this with:

[[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];

I then post a notification which RegisterViewController is listening for:

[[NSNotificationCenter defaultCenter] postNotificationName:UserHasCompletedQuestionnaireNotification object:nil];

Now, here is where my problem lies - When RegisterViewController hears the notification I try and dismiss it, however cannot.

I have tried various connotations of dismissing it such as:

[[self tabBarController] dismissViewControllerAnimated:YES completion:nil];

which does nothing, and:

[[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];

which gives the following errors:

attempt to dismiss modal view controller whose view does not currently appear. self = <RegisterViewController: 0x8525ea0> modalViewController = <UINavigationController: 0x74545e0>
attempt to dismiss modal view controller whose view does not currently appear. self = <UITabBarController: 0x8320060> modalViewController = <RegisterViewController: 0x8525ea0>

Hopefully someone is going to be able to tell me I am doing something stupidly wrong here.

Thanks, Nick

Instead of:

[[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];

If you want to dismiss both controllers at the same time, you can eliminate the notification, and do this (from QuestionnaireViewController):

[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];

If I understand your structure correctly, self.presentingViewController.presentingViewController should be the controller in the first tab that presented RegisterViewController. Dismissing it also dismisses anything that it presented.

尝试:

[self dismissViewControllerAnimated:YES completion:nil];

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