ios 以编程方式将标签栏控制器添加到当前应用程序流
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16396392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add a Tab Bar Controller Programmatically to current App Flow
提问by Akshay Goel
I want to add a tab Bar Controller to my current App Flow. Currently I have a page with a button which on clicking opens a new viewcontroller with a web view where the user logs in and after login I want to take him to his home Page where the navigation bar has his name and a logout button in the right. The home page should also have a tab bar with 3 different tabs. I am able to load the home page view from the webview and get the navigation bar. But I am unable to add the tabBar and get it working. I am confused as to where to add the code for adding TabBar. I am using the below code to add tab bar -
我想在我当前的 App Flow 中添加一个标签栏控制器。目前我有一个带有按钮的页面,单击该按钮会打开一个带有 Web 视图的新视图控制器,用户可以在其中登录,登录后我想带他到他的主页,其中导航栏有他的名字,右侧有一个注销按钮. 主页还应该有一个包含 3 个不同选项卡的选项卡栏。我能够从 webview 加载主页视图并获取导航栏。但我无法添加 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];
Currently I have put the above code in the viewDidLoad of a ViewController TabViewController which extends UITabBarController. In my webView controller I have put the following code -
目前我已经把上面的代码放在一个 ViewController TabViewController 的 viewDidLoad 中,它扩展了 UITabBarController。在我的 webView 控制器中,我放置了以下代码 -
TabViewController *tab=[[TabViewController alloc] init];
tab.userInfo=userInfo;
[self presentViewController:tab animated:YES completion:nil];
But the app crashes as soon as I click any tab other than the one already open. Please Help.
但是,只要我单击已打开的选项卡以外的任何选项卡,应用程序就会崩溃。请帮忙。
回答by andrew lattis
The way I've done this in the past is to create a UITabBarController
subclass that contains all of the tabBar
creation code you have above.
我过去这样做的方法是创建一个UITabBarController
包含上面所有tabBar
创建代码的子类。
Then use your UINavigationController
to push the tabBar
subclass to the screen.
然后使用您UINavigationController
的将tabBar
子类推送到屏幕。
Here's a sample of my UITabBarController
subclass:
这是我的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];
}
回答by Manjeet
Set Delegate UITabBarDelegate
设置委托 UITabBarDelegate
here TabBar Viewcontroller image http://prntscr.com/ba5oks
这里 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;
}
}