views:

185

answers:

2

I need to display a couple of view controllers (eg, login screen, registration screen etc). What's the best way to bring each screen up?

Currently for each screen that I'd like to display, I call a different method in the app delegate like this: Code:

- (void) registerScreen
{
 RegistrationViewController *reg = [[RegistrationViewController alloc] initWithNibName:@"RegistrationViewController" bundle:nil];
 [window addSubview:reg.view]; 
}
- (void) LoginScreen
{
 LoginViewController *log = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
 [window addSubview:log.view]; 
}

It works, but I cant imagine it being the best way.

+1  A: 

I've often wondered if this is the best way myself, but when I'm not using IB's built-in stuff (like a NavigationController) I have a single method in the AppDelegate, switchToViewController:(UIViewController *)viewController that I pass...well, it's pretty self-explanatory I guess. This way there's only one place where it's done, and I can easily define transitions in that method once the app nears completion.

Also, don't forget to remove the previous views in your methods, otherwise you're liable to run out of memory. Something like this:

-(void) switchToViewController:(UIViewController *)c {
    if(c == currentController) return;

    [currentController.view removeFromSuperview];
    [window addSubview:c.view];
    [currentController release];
    currentController = [c retain];
}
Ian Henry
+1  A: 

I'd recommend reading the View Controller Programming Guide if you haven't: http://developer.apple.com/iphone/library/featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html

It sounds like presenting a view controller modally may be your best bet - but you'll probably want to wrap it in an UINavigationController first.

eg

UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:theControllerYouWantToPresent] autorelease];
[self presentModalViewController:navController animated:YES];
blindJesse
Life is so much simpler when you use navigation controllers...
Kendall Helmstetter Gelner