I am developing an application where i need to show a list as a menu(Courses,lessons,grade,logout) to the user. so even before this i need to show a login screen. Only upon successful and valid login i need to re-direct the user to the menu. So i have planned to develop a tabBar based application with 4 tabs. Here i am confused on how to add the login view controller even before the TabBar controller is loaded. I want the first tab to be selected every time. As of now i am adding my TabBar controller as a rootviewcontroller to my AppDelegate window and then presenting the login view controller as a modal view controller. But the problem here is even before the Login View controller is loaded, my courses view controller is loaded because the tabbarcontroller is loaded first. My actual requirement is i need to load the course view controller with the list of courses based on the inputs given in the Login View controller. But loadview of course view controller is loaded even before the load view of login view controller. so my list of courses is always the same irrespective of who logs in. I am confused here on how to move forward...Any suggestion here would be of great help...

link|improve this question

See my response to a similar question, maybe could helps you. – Mat 2 hours ago
@Mat: But the problem here is Apple insists that TabBarcontroller is the rootview controller...In ur answer u mentioned that show the login view controller as the root view controller and later assign the tabbar controller as the rootview controller...and my question here is when i am in login view controller, how can i add the tabbar controller as the root view controller in my login view controller? – Pradeep Reddy Kypa 2 hours ago
I meant that the first vc view will be your loginVC as rootVC (now there is no tabBar), then you have to swap the rootVC from your loginVC to tabBarVC and load your data based on the user's input. I don't know how Apple can understand who is the rootVC if you start with a window-based template. – Mat 2 hours ago
Ur suggestion seems to be valid but can u please provide a sample code snippet or any tutorial link where this is expalined..It would make my understanding clear if i have a look at it..Thanks a lot Mat for ur time. Ur suggestion was really a different idea for me... – Pradeep Reddy Kypa 1 hour ago
i've provided you a quick example. – Mat 1 hour ago
show 1 more comment
feedback

4 Answers

first set your LoginViewController as rootViewController and then, set the tabBarController as rootViewController. Hope it will work :)

link|improve this answer
When my app loads for the first time i will set the login view controller as my rootViewController but where is that i need to set the tabBarController as rootViewController? I am confused on this... – Pradeep Reddy Kypa 1 hour ago
feedback

So, a very quick example, could be; in your loginViewController you should have some method something like this:

//Call this after the user has done with the login
-(IBAction)remove:(id)sender{
    AppDelegate *del=(AppDelegate*)[[UIApplication sharedApplication] delegate];
    //Set some data based on the user's input (eg some property shared in the AppDelegate)
    //del.dataEnterByTheUser=someData;
    [del removeLoginView];
} 

Then in your AppDelegate (assuming that now the rootViewController is the loginViewController) you could do like this (you can optimize the transition):

-(void)removeLoginView{

    UITabBarController *tabVC=[[UITabBarController alloc] init];
    ViewController *v1=[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    //v1.data=self.dataEnterByTheUser;
    ViewController *v2=[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    NSArray *arrayVC=[NSArray arrayWithObjects:v1,v2, nil];
    [tabVC setViewControllers:arrayVC];
    [tabVC setSelectedViewController:0];
    //self.viewController is the loginViewController
    CGRect rectVC=self.viewController.view.frame;
    rectVC.origin.y=480;
    [UIView animateWithDuration:0.3f delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.viewController.view.frame=rectVC;
    } completion:^(BOOL finished){
        [self.viewController.view removeFromSuperview];
        self.viewController=nil;
        self.window.rootViewController=tabVC;
    }];    
}

Also remember to set in each viewControllers's initWithNibName: set the self.title to set the title on the tabItem.

link|improve this answer
feedback

No need to fiddle around with the rootViewController...

Just add the following code at the beginning of your viewWillAppear: method of the view controller which would normally appear first (most likely the VC you are presenting in the first tab):

[self.tabBarController presentModalViewController:loginController animated:NO];

Where loginController is obviously the view controller which manages your login screen. If you show it without animation, it will be the first thing visible when your app launches (after the default image disappears). I've used the same method to show a disclaimer page which the user must read before using the app. It's working just fine and made it to the store without problems.

Edit: In this solution, the loginController must dismiss itself once the user has successfully logged in:

[self dismissModalViewControllerAnimated:NO]; //Although you might do this animated, this time
link|improve this answer
feedback

i have developed demo application i think this may help you what you want.

you can get it from here

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.