How to develop a TabBar based application with a l

2019-07-12 18:16发布

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

3条回答
乱世女痞
2楼-- · 2019-07-12 18:29

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.

查看更多
趁早两清
3楼-- · 2019-07-12 18:29

You can just change the array of view controllers in the tab bar controller at runtime. That should be sufficient for your purposes.

I've written a small example. Try to login with the following credentials:

  • username: john, password: doe
  • username: pete, password: poe

You will see a different combination of tabs depending on the login used.

The example can be downloaded from my Dropbox: http://dl.dropbox.com/u/6487838/LoginTabExample.zip

查看更多
淡お忘
4楼-- · 2019-07-12 18:41

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
查看更多
登录 后发表回答