views:

249

answers:

1

Hello!

I build my first iPhone application and I have a problem with switching views. First, I have two views (login, registration) which switch via "presentModalViewController:animated:".

But if someone logged in, there must be a new kind of view. I wanna have an UITabBar at the bottom (tab bar controller). But this does not work. I tried to create a new AppDelegate, so that I can use tutorials like this one which need a AppDelegate:

http://www.youtube.com/watch?v=LBnPfAtswgw&feature=player_embedded

The switch to the new controller is done like this:

startViewController = [[StartViewController alloc] initWithNibName:@"StartView" bundle:nil];
[UIView beginAnimations:@"View Curl" context:nil];
[UIView setAnimationDuration:2.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
[self.view addSubview:startViewController.view];
[UIView commitAnimations];

The screen is white, because the shown view is the UIView in my StartView.xib. There I have the new AppDelegate, File's owner, View, TabBarController. But only the UIView is loaded and not the TabBarController.

Do you have an idea how I could this problem?

Thanks & Best Regards.

+3  A: 

I might suggest you start with a TabBarController, and if the username/password is not set, the active ViewController executes presentModalViewController:animated: to display the login/registration viewsControllers in modal mode (hidding the underliying TabBarController).

Here is some sample code for doing it programmatically.

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
 self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
 [window setBackgroundColor:[UIColor whiteColor]];

 tabBarController = [[UITabBarController alloc] init];


 aViewController = [[aViewController alloc] init];
 UINavigationController *aNavController = [[[UINavigationController alloc] initWithRootViewController:aViewController] autorelease];
 aNavController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
 [aViewController release];

 tabBarController.viewControllers = [NSArray arrayWithObjects: aNavController, nil];

 // Add the tab bar controller's current view as a subview of the window
    [window addSubview:tabBarController.view];

 if(userNotLoggedIn){
     [self displayLoginViewController];
 }


    [window makeKeyAndVisible];
}

- (void)displayLoginViewController {
 LoginViewController *controller = [[LoginViewController alloc] init];
 // setup controller
 [self.tabBarController presentModalViewController:controller animated:NO];
 [controller release];
}
Benjamin Ortuzar
Thanks a lot for your reply.But sorry, I do not know what you exactly mean. I should create a TabBarController in MainWindow.xib. So if I start the app, the TabBar at the bottom will be shown and not my login view or the registration view?!
Tim
Exactly, when the app starts the TabBarController will be loaded, and from the firstViewController included in the TabBarController, you check if the username/pass, if its not set, execute presentModalViewController to display the loginViewController, it should be so quick that the TabBarControllwe will hardly be seen generating the effect you need.
Benjamin Ortuzar
Yes, but, if the app starts, the login screen will be shown. The user has to log in first, so the first screen always is the login screen. I need to switch it if someone logged into successfully.
Tim
The login screen would be pushed immediatley after the app is started on modal mode (on top of the TabBarController). So when the user opens the app and its not logged in, he will be have to login or register and if he is already loggedIn/registered the modalViewcontroller wont be displayed and he will just see the TabBarController.
Benjamin Ortuzar
The user always have to log in! He is never already logged in when the application starts.I do not understand how I switch from the login view to the TabBarController View.
Tim
I added some sample code you could reuse. I dont use IB so I hope it helps.
Benjamin Ortuzar
I wrote which works, so I see the Login-Screen first.- (void)applicationDidFinishLaunching:(UIApplication *)application { // Override point for customization after application launch [window addSubview:rootController.view]; LoginViewController *controller = [[LoginViewController alloc] init]; // setup controller [self.rootController presentModalViewController:controller animated:NO]; [controller release]; [window makeKeyAndVisible];}But what I have to write to the login method in the LoginViewController to get the TabBarController-View?
Tim
To dismiss the modal view controller you use - (void)dismissModalViewControllerAnimated:(BOOL)animated You can find more info here: http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/dismissModalViewControllerAnimated:
Benjamin Ortuzar