adit
adit

Reputation: 33674

dismissing modalViewController of modalViewController

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>

Upvotes: 1

Views: 9362

Answers (3)

miex
miex

Reputation: 64

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....
}

Upvotes: 2

topace
topace

Reputation: 1791

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

Upvotes: 8

superjessi
superjessi

Reputation: 1790

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];

Upvotes: 13

Related Questions