使用UITabBarController创建Tabbar获取tabBarItem的点击方法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用UITabBarController创建Tabbar获取tabBarItem的点击方法相关的知识,希望对你有一定的参考价值。

项目中有涉及到拨号键盘的弹出,想要在点击控制器的时候键盘回收,再点击下面的Tabbar实现键盘出现.由于正常情况下,当在当前界面的时候,点击下面的tabbar是不再相应的.这里就需要找到能执行tabbar点击事件的代理方法.

第一步:需要在主控制器执行代理UITabBarControllerDelegate;

第二步:执行代理方法:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController.

这样的话即使是一直在此界面,点击对应的tabbar也会走这一步,可以在此方法写自己想要执行的操作.

举例代码如下:

第一步:设置代理:

@interface RootViewController ()<UITabBarControllerDelegate>

第二步:设置UITabBarController.

 1 ViewController1 *hosterVC = [[ViewController1 alloc]init];
 2     UINavigationController *hosterNC = [[UINavigationController alloc]initWithRootViewController:hosterVC];
 3     hosterNC.navigationBar.translucent = NO;
 4     
 5     
 6     ViewController2 *menuListVC = [[ViewController2 alloc]init];
 7     UINavigationController *menuListNC = [[UINavigationController alloc]initWithRootViewController:menuListVC];
 8     menuListNC.navigationBar.translucent = NO;
 9     
10     ViewController3 *healthMainVC = [[ViewController3 alloc]init];
11     UINavigationController *healthMainNC = [[UINavigationController alloc]initWithRootViewController:healthMainVC];
12     healthMainNC.navigationBar.translucent = NO;
13     
14     ViewController4 *zhiShiVC = [[ViewController4 alloc]init];
15     UINavigationController *zhiShiNC = [[UINavigationController alloc]initWithRootViewController:zhiShiVC];
16     zhiShiNC.navigationBar.translucent = NO;
17     
18     ViewController5 *sanCanVC = [[ViewController5 alloc]init];
19     UINavigationController *sanCanNC = [[UINavigationController alloc]initWithRootViewController:sanCanVC];
20     sanCanNC.navigationBar.translucent = NO;
21     
22     UITabBarController *rootVC = [[UITabBarController alloc]init];
23     rootVC.viewControllers = @[menuListNC,healthMainNC,hosterNC,zhiShiNC,sanCanNC];
24     
25     UITabBarItem *item1 = [[UITabBarItem alloc]initWithTitle:@"菜单" image:[UIImage imageNamed:@"[email protected]"] tag:101];
26     UITabBarItem *item2 = [[UITabBarItem alloc]initWithTitle:@"养生" image:[UIImage imageNamed:@"[email protected]"] tag:102];
27     UITabBarItem *item3 = [[UITabBarItem alloc]initWithTitle:@"我的" image:[UIImage imageNamed:@"iconfont-wode-4.png"] tag:103];
28     UITabBarItem *item4 = [[UITabBarItem alloc]initWithTitle:@"知识" image:[UIImage imageNamed:@"[email protected]"] tag:104];
29     UITabBarItem *item5 = [[UITabBarItem alloc]initWithTitle:@"三餐" image:[UIImage imageNamed:@"[email protected]"] tag:105];
30     menuListNC.tabBarItem = item1;
31     healthMainNC.tabBarItem = item2;
32     hosterNC.tabBarItem = item3;
33     zhiShiNC.tabBarItem = item4;
34     sanCanNC.tabBarItem = item5;
35     
36     self.window.rootViewController = rootVC;

第三步:执行代理方法:

 1 #pragma  mark -- UITabBarDelegate
 2 - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
 3 {
 4     
 5     if (tabBarController == self.tabbar)
 6     {
 7         
 8         if ([viewController.tabBarItem.title isEqualToString:@"菜单"])
 9         {
10             NSLog(@"执行你想要点击此tabBarItem执行的操作");
11 
12         }
13     }
14 }

转载请注明出处.

 

以上是关于使用UITabBarController创建Tabbar获取tabBarItem的点击方法的主要内容,如果未能解决你的问题,请参考以下文章

以编程方式在 UITabBarController 和 UINavigationController 中呈现视图

替换 UITabBarController 中的 UIViewController

UITabbarController 设置视图

更改 UITabBar 高度

将另一个视图控制器推送到 UITabBarController 视图中

2 ViewControllers 以模态方式呈现 UITabBarController