IOS login interface with TabBar interface switch

Problem description

Normally App would contain TabBar, and TabBar is created in AppDelegate and is set to RootViewController, if a certain products need to use to log in the case (QQ, WeChat), the first page so users should see the login page, then the login page is set to RootViewController, then click how to switch to the login button with TabBar page?

There are small partners will say that the direct push in the past can ah, but is it true

One *one = [One new]; UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:one]; Two *two = [Two new]; UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:two]; Three * three = [Three new]; UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:three]; Four *four = [Four new]; UINavigationController *nav4 = [[UINavigationController alloc] initWithRootViewController:four]; NSArray *list = [NSArray arrayWithObjects:nav1, nav2, Nav3, nav4 self.viewControllers = list, nil];

A TabBarController has 4 ViewController, although the simple push can jump past, but after the jump in the past ViewController is not controlled by the TabBarController, so this method is not feasible

Terms of settlement

  1. In the AppDelegate by judging whether there is token, representing whether the user is logged in, and then set a different RootViewController NSUserDefaults *user = [NSUserDefaults standardUserDefaults]; TokenID [user objectForKey:@ = “UserToken”]; UserID [user objectForKey:@ = “UserID”]; if (TokenID.length = = 0) {LoginView *login = [[LoginView alloc] init]; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:login]; self.window.rootViewController = NAV;} else {RootTabBar *tabs = [[RootTabBar alloc] init]; self.window.rootViewController = tabs;}
  2. The root view controller using switching method in the login login click event will be achieved in the root view controller AppDelegate switch / / *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate]; RootTabBar *tabs = [[RootTabBar alloc] init]; appDelegate.window.rootViewController = tabs;
  3. Similarly, when any one token interface failure, according to the process, the user must be logged on to use function, switching operation of any one user at this time will be the root view, let him enter the login interface, use the above method.


The login process is simply my personal opinion, there should be a better solution for the first time to write an article, is the record he had met with difficulties, there is a need for reference, you point a praise it, than the heart.