首先,設置第二個選項卡的AccountViewController
。然後,在tabBarController:didSelectViewController:
回調,檢查用戶是否登錄如果用戶沒有登錄,用LoginViewController
更換的ViewController:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if ([tabBarController selectedIndex] == 1 && !user.isSignedIn)
{
//fill an array with all the view controllers in the UITabBarController
NSMutableArray *arr = [[NSMutableArray alloc] initWithArray:[tabBarController viewControllers]];
//inistantiate login view controller
UIViewController *loginViewController = [[LoginViewController] init];
//replace the second UIViewController in the array with LoginViewController
[arr replaceObjectAtIndex:1 withObject: loginViewController];
//set array
[tabBarController setViewControllers:arr];
}
}
用戶登錄,在以後,隨着更換第二個標籤AccountViewController