简体   繁体   中英

Push to UITabBar from ViewController

I've created the appDelegate method below. This creates an tabBar for the 3 viewcontrollers FixtureViewController, WorldCupViewController and MenuViewController. My RootController is set to the loginViewController which will make sure that your logged in before going to next ViewController. When a person is logged in i would like to push it to the tabBarController ? How can i do this.

I've tried in the loginViewController to push to the firsttab which is MenuViewController, but this wont show the UITabBar.

tabBarController = [[UITabBarController alloc] init];

MenuViewController *firstTab = [[MenuViewController alloc] initWithNibName:@"MenuViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:firstTab];


FixtureViewController *secondTab = [[FixtureViewController alloc] initWithNibName:@"FixtureViewController" bundle:nil];
UINavigationController *navController2 = [[UINavigationController alloc] initWithRootViewController:secondTab];

WorldCupViewController *thirdTab = [[WorldCupViewController alloc] initWithNibName:@"WorldCupViewController" bundle:nil];
UINavigationController *navController3 = [[UINavigationController alloc] initWithRootViewController:thirdTab];

LoginViewController *loginView = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
UINavigationController *navController4 = [[UINavigationController alloc] initWithRootViewController:loginView];



self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = @[navController, navController2, navController3];


[self.window setRootViewController:navController4];
[self.window makeKeyAndVisible];

I believe you'll want to push the entire tabBarController, not just the first tab. So in your LoginViewController:

#import "YourAppDelegate.h"

...

-(void)someMethod {
    YourAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    UITabBarController *tabBarController = appDelegate.tabBarController;
    [self.navigationController pushViewController:tabBarController animated:YES];
}

Note that this assumes you have a publicly visible UITabBarController property in your AppDelegate's interface(.h) file.

you try this :

NSArray* newArray = [NSArray arrayWithArray: youTabBar.viewControllers];
 [youTabBar setViewControllers:newArray animated:YES];
 [youTabBar setSelectedIndex:0];
 [self.window setRootViewController: youTabBar];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM