简体   繁体   中英

How to dismiss multiple presentModalViewControllers and get back to the root Tab Bar controller?

I have an application that shows a presentModalViewController upon launch.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch.

    // Add the tab bar controller's view to the window and display.
    [self.window addSubview:tabBarController.view];
    Overview *overviewViewController = [[Overview alloc] initWithNibName:@"Overview" bundle:nil];
    [self.tabBarController presentModalViewController:overviewViewController animated:YES];
    [overviewViewController release];
    [self.window makeKeyAndVisible];
    return YES;
}

Once the overviewController is shown, the user gets to log in or register. If they select login, then I am using another presentModalViewController that lets them log in:

  -(IBAction) btnLoginPressed{

//  [self dismissModalViewControllerAnimated:YES];
    Login *loginOverView = [[Login alloc] initWithNibName:@"Login" bundle:nil];
    [self presentModalViewController:loginOverView animated:YES];
    [loginOverView release];


}

However on a successful login, I want both the presentModalViewController to disappear allowing me to get back to the root controller which is a tab bar controller.

I have tried to do the following but it does not work:

-(IBAction) btnSubmitLoginPassword{

    //make web service call
//  [self dismissModalViewControllerAnimated:YES];
    [self.tabBarController dismissModalViewControllerAnimated:YES];
}

Now in my Googling, I have come across the concept of delegates which I am not familiar with. Can someone take the time to help me in my dilemma.

Thanks in advance

The view controllers are organized in a stack. You use the UINavigationController methods popToRootViewControllerAnimated: or popToViewController:animated: to control how many views to pop off the top of the stack.

You can get to the UINavigationController instance through your application delegate.

To pop all view controllers to the root view controller: (which I think is what you are asking)

UIApplicationDelegate* delegate = [[UIApplication sharedApplication] delegate];
[delegate.navigationController popToRootViewControllerAnimated:YES];

To pop all view controllers to a known view controller in the stack:

UIApplicationDelegate* delegate = [[UIApplication sharedApplication] delegate];
[delegate.navigationController popToViewController:popToViewController animated:YES];

Add id delegate; and @property (nonatomic, retain) id delegate; in your Overview.h. Add @synthesize delegate in your Overview.m. Then add the following after your initWithNibName:bundle :

[overviewViewController setDelegate: self];

Do the same for your Login class: Add id delegate; and @property (nonatomic, retain) id delegate; in your Login.h. Add @synthesize delegate in your Login.m. Then add the following after your initWithNibName:bundle :

[overviewViewController setDelegate: self];

Add the following method in your Overview.m:

- (void)dismissLoginView {
    [self dismissModalViewControllerAnimated: NO];
    [delegate dismissModalViewControllerAnimated: YES];
}

Change your -(IBAction) btnSubmitLoginPassword to

-(IBAction) btnSubmitLoginPassword {
    [delegate dismissLoginView];
}

I haven't tested it. I hope it works! Let me know when not.

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