Govind
Govind

Reputation: 2348

Design View Hierarchy Tabbar bar based iOS application

Still getting confused when starting a new project. I am having a tab bar application with a number of tabs in it and a navigation controller associated with each tab.

And I need to add a log in screen before the tabbar. Also need to take the user to the tabbar when he is signed in once. ie, Once the user signed in, every time he is directed to the tabbar unless he logs out. I need to choose a pattern.

  1. Create a login screen and present the tabbar controller modally and dismiss the modal view controller when user logs out.
  2. Changing the window.rootViewController to the tabbarcontroller when the user signs in and if not set the window.rootview controller as the login view controller.
  3. In app did finish launching, [window addSubview:tabcontroller.view] and then [window addSubview:loginviewcontroller.view] and hiding the loginviewcontroller when the user successfully logged in
  4. Just opposite to my option 1, present tab bar controller and present the loginview controller modally if user is not logged in and dismissing when logs in.

Kindly choose me a best option or any other way to do it better.

Upvotes: 1

Views: 753

Answers (1)

kulss
kulss

Reputation: 2055

     **Changing the window.rootViewController to the tabbarcontroller
     when the user signs in and if not set the window.rootview controller 
     as the login view controller.** 

is the best way to do this thing, Initially If user is not login than LoginViewController will be the RootViewController else tabBarController will be RootViewController and As user logout change the RootViewController to LoginViewController.

UPDATE : Try This, this will work. If it is not clear to understand tell me i will send u a working project.

Bydefault InitialViewController from your Main Storyboard is instantiated and displayed automatically when your app starts up. To prevent this happening you need to remove the UIMainStoryboardFile setting from your info.plist file.

With no default view controller, you are now free to create one programmatically at app start.

Here I am writing a small example below this line. Create two view controller without xib FirstViewController and SecondViewController and add than in MainStoryboard_iPhone and In Appdelegate.m implement these methods.

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

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

/*
 call any of the two methods below, it will change the RootViewController accordingly
 */

// [self firstRootViewController];  // make FirstViewController Root view controller
[self secondRootViewController];    // make SecondViewController Root view controller

self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;

}

- (void) firstRootViewController {

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
FirstViewController *first = [sb instantiateViewControllerWithIdentifier:@"FirstViewController"];
[self.window setRootViewController:first];

}

- (void) secondRootViewController {

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
SecondViewController *second = [sb instantiateViewControllerWithIdentifier:@"SecondViewController"];
[self.window setRootViewController:second];

}

Now set the viewController in MainStoryboard_iPhone.storyboard

enter image description here

Upvotes: 3

Related Questions