Search code examples
iphoneiosauthenticationtabbarcontroller

How to develop a TabBar based application with a login functionality?


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...


Solution

  • 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];
        CGRect rectVC=self.loginViewController.view.frame;
        rectVC.origin.y=self.view.frame.size.height;
        [UIView animateWithDuration:0.3f delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            self.loginViewController.view.frame=rectVC;
        } completion:^(BOOL finished){
            [self.loginViewController.view removeFromSuperview];
            self.loginViewController=nil;
            self.window.rootViewController=tabVC;
        }];    
    }
    

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