切换标签栏时检测选定的标签栏
Posted
技术标签:
【中文标题】切换标签栏时检测选定的标签栏【英文标题】:Detect selected tabbar when switching tab bar 【发布时间】:2013-01-16 09:45:05 【问题描述】:我有三个标签栏应用程序,我的标签是 TAB1、TAB2、TAB3 我在 TAB1 视图控制器中编写了以下代码来检测用户按下了哪个标签
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
NSLog(@"tab selected: %@", item.title);
但是这个委托永远不会被调用
我已经在 appdelegate.m 中设置了类似的选项卡
- (void)setupTabBar
self.myWorkListViewController = [[MyWorkListViewController alloc] initWithNibName:@"MyWorkListViewController"
bundle:nil];
self.askHRViewController = [[AskHRViewController alloc] initWithNibName:@"AskHRViewController"
bundle:nil];
self.moreViewController = [[MoreViewController alloc] initWithNibName:@"MoreViewController"
bundle:nil];
self.bookLeaveViewController = [[BookLeaveViewController alloc] initWithNibName:@"BookLeaveViewController"
bundle:nil];
self.helpViewController = [[HelpViewController alloc] initWithNibName:@"HelpViewController"
bundle:nil];
// Create navigation controllers
workListNavigationController = [[UINavigationController alloc] initWithRootViewController:self.myWorkListViewController];
askHRNavigationController = [[UINavigationController alloc] initWithRootViewController:self.askHRViewController];
bookLeaveViewController = [[UINavigationController alloc] initWithRootViewController:self.bookLeaveViewController];
moreNavigationController = [[UINavigationController alloc] initWithRootViewController:self.moreViewController];
helpNavigationController = [[UINavigationController alloc] initWithRootViewController:self.helpViewController];
[self setTabBarImagesAndText];
// Setup tab bar controller
self.tabBarController = [[UITabBarController alloc] init];
[self.tabBarController setViewControllers:[NSArray arrayWithObjects:workListNavigationController, askHRNavigationController, bookLeaveViewController, helpNavigationController,moreNavigationController, nil]];
//Set TabBar background
[self.tabBarController.tabBar insertSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TabBar_ios4_Background"]] atIndex:0];
[self.tabBarController setSelectedIndex:0];
【问题讨论】:
【参考方案1】:您可以像这样检测选定的 Tabbar 项:- 作为你的代码,你只需要添加这一行
// Setup tab bar controller
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.delegate=self;
[self.tabBarController setViewControllers:[NSArray arrayWithObjects:workListNavigationController, askHRNavigationController, bookLeaveViewController, helpNavigationController,moreNavigationController, nil]];
//Set TabBar background
[self.tabBarController.tabBar insertSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TabBar_iOS4_Background"]] atIndex:0];
[self.tabBarController setSelectedIndex:0];
在.h文件中定义类似
@interface yourViewcontroller : UIViewController<UITabBarControllerDelegate>
//declare your Tabbar controller with @proparty
在.m文件中
//@synthesize here your Tabbar controller
- (void)viewDidLoad
self.yourTabbarControler.delegate=self;
[super viewDidLoad];
现在把这个 UITabbarController 的委托放上去
- (void)tabBarController:(UITabBarController *)tabBarController
didSelectViewController:(UIViewController *)viewController
NSLog(@"controller class: %@", NSStringFromClass([viewController class]));
NSLog(@"controller title: %@", viewController.title);
if (viewController == tabBarController.moreNavigationController)
tabBarController.moreNavigationController.delegate = self;
【讨论】:
这个委托方法也没有在 TAB1viewcontroller 中被调用:( 谢谢 :) :) 但我很惊讶我必须在每个控制器中设置委托。我们不能一次做这个设置吗? 看到这个didSelectViewController
它是UITabbarController
的委托方法,所以每当您需要使用时,您必须声明特定 UItabbarController 的委托,这就是原因【参考方案2】:
您必须使用代码(在 ViewDidLoad 等中)或在界面构建器中“连接”委托。 看看这个解释如何连接 textView 委托的答案(几乎相同):https://***.com/a/1785366/764575
【讨论】:
以上是关于切换标签栏时检测选定的标签栏的主要内容,如果未能解决你的问题,请参考以下文章
再次点击当前标签栏时如何在 UITableView 中滚动到顶部?
如何让自定义标签栏显示标签栏项目在 Xcode 中设置的选定图像?
位置:当显示标签栏时,固定“hitboxes”在大尺寸手机上的 iOS 10 Safari 中向上移动
如何限制从顶部定位我的标签,但在隐藏导航栏时不让它移动(因为它会调整视图高度)?