dismissing modalViewController of modalViewController

余生长醉 提交于 2019-12-02 07:27:03

问题


So I have a UITabBarController app and I want to display a login page, and so I did:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userDidLogin:) name:UserDidLoginNotification object:nil];
LoginViewController* loginViewController = [[LoginViewController alloc] init];
        self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:0];
        [self.tabBarController.selectedViewController presentModalViewController:loginViewController animated:NO];
        [loginViewController release];

Inside my LoginViewController I can as well show another modalViewController:

- (void) twitterLogin: (UIViewController *) askingView
{
    UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine: _twitter delegate: self];

    if (controller) {
        self.askingView = askingView;
        [askingView presentModalViewController: controller animated: YES];
    }
}

I have the following method where the askingView is the LoginViewController, when I want to dismiss this I do:

[self.askingView dismissModalViewControllerAnimated:YES];
    [[NSNotificationCenter defaultCenter] postNotificationName:UserDidLoginNotification object:nil];

However, this doesn't dismiss the LoginViewController and show the UITabBarController views.. it just dismisses my modalViewController shown from the LoginvVIewController. What am I doing wrong here? I am also getting the following error:

attempt to dismiss modal view controller whose view does not currently appear. self = <LoginViewController: 0x2aff70> modalViewController = <SA_OAuthTwitterController: 0x2d2a80>
2011-09-16 09:45:37.750 VoteBooth[4614:707] attempt to dismiss modal view controller whose view does not currently appear. self = <MainViewController: 0x29fec0> modalViewController = <LoginViewController: 0x2aff70>

回答1:


In order to dismiss a modal view that is presented over another modal view, you have to call dismissModalViewControllerAnimated: on the parent of the parent. I have used this in some of my apps and it has worked beautifully for me (after many painstaking hours trying to figure it out). Here is exactly what I've used:

[[[self parentViewController] parentViewController] dismissModalViewControllerAnimated:YES];



回答2:


if ([self respondsToSelector:@selector(presentingViewController)]) {
    [self.presentingViewController.presentingViewController dismissModalViewControllerAnimated:YES]; // for IOS 5+
} else {
    [self.parentViewController.parentViewController dismissModalViewControllerAnimated:YES]; // for pre IOS 5
}



回答3:


If you have a dynamic UX and do not know how many parents to go to, you can use this recursive function to figure it out...

- (void) goHome
{
    //Dismiss modal back to home page
    int numberOfVcsToDismiss = [self findRootViewController:self];
    [self dismissToRootVc:numberOfVcsToDismiss];
}

- (int) findRootViewController:(UIViewController*)vc
{
    if(vc)
    {
        return 1 + [self findRootViewController:vc.presentingViewController];
    }
    return 0;
}

- (void) dismissToRootVc:(int)count
{
    if(count == 1)
        [self dismissViewControllerAnimated:YES completion:^{}];
    if(count == 2)
        [self.presentingViewController dismissViewControllerAnimated:YES completion:^{}];
    if(count == 3)
        [self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:^{}];
    if(count == 4)
        [self.presentingViewController.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:^{}];
    if(count == 5)
        [self.presentingViewController.presentingViewController.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:^{}];
    //etc....
}


来源:https://stackoverflow.com/questions/7448013/dismissing-modalviewcontroller-of-modalviewcontroller

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