第二个视图中的Tabbar

我在我的应用程序中有一个激活页面,每个用户都必须激活该应用程序。 一旦应用程序被激活,用户移动到标签栏视图。

我创build了一个标签栏应用程序,从我的activationView上点击button我试图调用标签栏,我得到一个完整的黑屏。

- (IBAction)moveToActivateView:(id)sender { AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; UITabBarController *tabBarController = [[UITabBarController alloc]init]; [self.view addSubview:tabBarController.view]; appDelegate.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil]; UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; self.tabBarController.viewControllers = @[viewController1, viewController2]; appDelegate.window.rootViewController = self.tabBarController; [appDelegate.window makeKeyAndVisible];} 

嘿让你的tabBarController的属性在AppDelegate和分配所有的ViewController那里然后调用你的tabView控制器从你的ViewController

在AppDelegate.h中

 @property (nonatomic, retain) IBOutlet UINavigationController *navigationController; @property (retain, nonatomic) IBOutlet UITabBarController *tabBarController; 

在AppDelegate.m中

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; [self makeTabBar]; [self addInitialVIew]; [self.window makeKeyAndVisible]; return YES; } -(void)makeTabBar { tabBarController = [[UITabBarController alloc] init]; tabBarController.delegate=self; FirstViewController *firstVC =[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil]; UINavigationController *firstNC = [[UINavigationController alloc] initWithRootViewController:firstVC]; firstNC.tabBarItem.title=@"Profile"; firstVC.tabBarController.tabBar.tag = 0; SecondViewController *secondVC = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; UINavigationController *SecondNavController = [[UINavigationController alloc] initWithRootViewController:secondVC]; SecondNavController.tabBarItem.title = @"Search"; secondVC.tabBarController.tabBar.tag = 1; NSArray *viewControllers =[[NSArray alloc]initWithObjects:firstNC,SecondNavController, nil]; [tabBarController setViewControllers:viewControllers animated:NO]; } -(void) addInitialVIew { InitialViewController *initialViewController = [[InitialViewController alloc]initWithNibName:@"InitialViewController" bundle:nil]; navigationController = [[UINavigationController alloc]initWithRootViewController:ViewController]; [self.window addSubview:navigationController.view]; } 

现在在InitialViewController您可以添加您的TabBar并删除InitialViewController

 - (IBAction)switchToTabBarBtnPress:(id)sender { AppDelegate *appdelegte =(AppDelegate*)[[UIApplication sharedApplication]delegate]; [[[appdelegte navigationController] view]removeFromSuperview]; [[appdelegte window]addSubview:[[appdelegte tabBarController]view]]; [[appdelegte tabBarController]setSelectedIndex:0]; } 

并按照我的答案

回答

你想创builddynamic的tabbar应用程序来解决你的问题。 所以你只需创build基于视图的应用程序。 在视图控制器的viewdidload方法中放入这些代码

 tabbar1 = [[UITabBarController alloc] init]; artist_tab_obj = [[artist_tab alloc] initWithNibName:@"artist_tab" bundle:nil]; UINavigationController *tabItem1 = [[[UINavigationController alloc] initWithRootViewController: artist_tab_obj] autorelease]; tabItem1.title=@"Artist"; tabItem1.tabBarItem.image=[UIImage imageNamed:@"Icon1.png"]; music_tab_obj = [[music_tab alloc] initWithNibName:@"music_tab" bundle:nil]; UINavigationController *tabItem2 = [[[UINavigationController alloc] initWithRootViewController: music_tab_obj] autorelease]; tabItem2.title=@"Music"; tabItem2.tabBarItem.image=[UIImage imageNamed:@"Icon2.png"]; shout_tab_obj = [[shout_tab alloc] initWithNibName:@"shout_tab" bundle:nil]; UINavigationController *tabItem3 = [[[UINavigationController alloc] initWithRootViewController: shout_tab_obj] autorelease]; tabItem3.title=@"Shout"; tabItem3.tabBarItem.image=[UIImage imageNamed:@"Icon3.png"]; schedule_tab_obj = [[schedule_tab alloc] initWithNibName:@"schedule_tab" bundle:nil]; UINavigationController *tabItem4 = [[[UINavigationController alloc] initWithRootViewController: schedule_tab_obj] autorelease]; tabItem4.title=@"Schedule"; tabItem4.tabBarItem.image=[UIImage imageNamed:@"Icon4.png"]; follow_tab_obj = [[follow_tab alloc] initWithNibName:@"follow_tab" bundle:nil]; UINavigationController *tabItem5 = [[[UINavigationController alloc] initWithRootViewController: follow_tab_obj] autorelease]; tabItem5.title=@"Follower"; tabItem5.tabBarItem.image=[UIImage imageNamed:@"Icon5.png"]; tabbar1.viewControllers = [NSArray arrayWithObjects:tabItem1, tabItem2,tabItem3,tabItem4,tabItem5,nil]; 

在用户接受是button操作调用此代码。

 [self.view insertSubview:tabbar1.view belowSubview: artist_tab_obj.view]; tabbar1.selectedIndex=1; [self presentModalViewController:tabbar1 animated:YES]; 

你知道你的本地varialbe tabBarController是不完全相同的,但有点隐藏属性self.tabBarController ? 您创build一个新的UITabBarCotnroller并将其分配给您的本地variablestabBarController ,只能从此方法访问。 然后你操作(添加新创build的视图控制器)等self.tabBarControllerSelf.tabBarController很容易在这里或其他任何东西。 但它不是你刚刚创build的UITabBarController

它是self.tabBarController (所以可能为零)你分配给窗口作为rootViewController

您将tabBarController's视图作为子视图添加到self.view中。 除此之外,我不知道自己的实际是什么,我不认为手动添加TabBar的视图作为子视图真的是必要的。 设置根视图控制器(正确)应该是足够的