以编程方式添加选项卡栏控制器到当前应用程序stream程

我想添加一个标签栏控制器到我目前的应用程序stream程。 目前我有一个页面上有一个button点击打开一个新的视图控制器与Web视图的用户login和login后,我想带他到他的主页导航栏有他的名字和注销button在右侧。 主页还应该有一个带有3个不同选项卡的标签栏。 我能够从web视图加载主页视图,并获得导航栏。 但我无法添加tabBar并得到它的工作。 我很困惑到哪里添加代码添加TabBar。 我正在使用下面的代码来添加标签栏 –

UITabBarController *tabBar = [[UITabBarController alloc] init]; HomeViewController *home = [[PPHomeViewController alloc] initWithUserName:[self.userInfo objectForKey:@"name"] Email:[self.userInfo objectForKey:@"email"] Phone:[self.userInfo objectForKey:@"phone_number"]]; home.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1]; UINavigationController *homeNavController = [[UINavigationController alloc]initWithRootViewController:home]; RequestViewController *req = [[RequestMoneyViewController alloc]init]; req.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:2]; UINavigationController *reqNavController = [[UINavigationController alloc]initWithRootViewController:req]; UIViewController *thirdViewController = [[UIViewController alloc]init]; thirdViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:3]; UINavigationController *thirdNavController = [[UINavigationController alloc]initWithRootViewController:thirdViewController]; UIViewController *fourthViewController = [[UIViewController alloc]init]; thirdViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:3]; UINavigationController *fourthNavController = [[UINavigationController alloc]initWithRootViewController:fourthViewController]; tabBar.viewControllers = [[NSArray alloc] initWithObjects:homeNavController, reqNavController, thirdNavController, fourthNavController, nil]; tabBar.delegate=self; tabBar.selectedIndex=0; UIImageView *homeImg=[[UIImageView alloc]initWithFrame:CGRectMake(0, 432, 80, 49)]; homeImg.tag=11; homeImg.image=[UIImage imageNamed:@"footer"]; UIImageView *reqImg=[[UIImageView alloc]initWithFrame:CGRectMake(81, 432,80, 49)]; reqImg.tag=12; reqImg.image=[UIImage imageNamed:@"footer"]; UIImageView *sendImg=[[UIImageView alloc]initWithFrame:CGRectMake(162, 432,80, 49)]; sendImg.tag=13; sendImg.image=[UIImage imageNamed:@"footer"]; UIImageView *localImg=[[UIImageView alloc]initWithFrame:CGRectMake(243, 432, 80, 49)]; localImg.tag=14; localImg.image=[UIImage imageNamed:@"footer"]; [tabBar.view addSubview:homeImg]; [tabBar.view addSubview:reqImg]; [tabBar.view addSubview:sendImg]; [tabBar.view addSubview:localImg]; [[[UIApplication sharedApplication]keyWindow]addSubview:tabBar.view]; 

目前我已经把上面的代码放在ViewController的TabViewController的viewDidLoad中,它扩展了UITabBarController。 在我的webView控制器我把下面的代码 –

 TabViewController *tab=[[TabViewController alloc] init]; tab.userInfo=userInfo; [self presentViewController:tab animated:YES completion:nil]; 

但是,一旦我点击了已经打开的标签以外的其他标签,应用程序就会崩溃。 请帮忙。

我过去的做法是创build一个UITabBarController子类,它包含上面所有的tabBar创build代码。

然后使用你的UINavigationControllertabBartabBar到屏幕上。

这里是我的UITabBarController子类的示例:

 - (void)viewDidLoad { [super viewDidLoad]; UIViewController *view1 = [[UIViewController alloc] init]; UIViewController *view2 = [[UIViewController alloc] init]; UIViewController *view3 = [[UIViewController alloc] init]; NSMutableArray *tabViewControllers = [[NSMutableArray alloc] init]; [tabViewControllers addObject:view1]; [tabViewControllers addObject:view2]; [tabViewControllers addObject:view3]; [self setViewControllers:tabViewControllers]; //can't set this until after its added to the tab bar view1.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"view1" image:[UIImage imageNamed:@"view1"] tag:1]; view2.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"view2" image:[UIImage imageNamed:@"view3"] tag:2]; view3.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"view3" image:[UIImage imageNamed:@"view3"] tag:3]; } 

设置委托UITabBarDelegate

这里TabBar ViewController图片http://prntscr.com/ba5oks

 #pragma mark- Tapbar delegate - (void)deselectTabBarItem:(UITabBar*)tabBar { tabBar.selectedItem = nil; } - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item { [self performSelector:@selector(deselectTabBarItem:) withObject:tabBar afterDelay:0.2]; switch (item.tag) { case 0: //perform action break; case 1: //do whatever you want to do. break; case 2: //call method break; default: break; } }