4

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

Strong Like Bull
  • 11,155
  • 36
  • 98
  • 169

2 Answers2

2

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];
Shane Powell
  • 13,698
  • 2
  • 49
  • 61
  • It will work on any type of view, altho I don't know what you mean by 'modal views'... – Shane Powell Apr 25 '11 at 18:27
  • I mean view controllers which have been loaded by calling the method presentModalViewController:animated. – dasdom Apr 26 '11 at 07:46
  • Sorry, I missed that you where using presentModalViewController. I actually don't know, but that want I understand... No. Why don't you change your code to use pushViewController:animated: instead? – Shane Powell Apr 26 '11 at 18:34
  • I am not the person who asked the question. – dasdom Apr 27 '11 at 09:45
1

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.

dasdom
  • 13,975
  • 2
  • 47
  • 58