禁用操作 - 用户点击选项卡栏项目以转到根视图控制器
我想在用户点击选项卡栏项目时禁用默认操作。
例如,我有一个带有 Tab1、Tab2 和 Tab3 的选项卡栏。在Tab1中,用户可以从View1导航到View3(View1>View2>View3)。如果用户位于 View3,并且点击 Tab1,应用程序会将用户带到 View1(根视图控制器)。我想禁用此功能。我不希望点击 Tab1 来弹出所有视图控制器。我怎样才能做到这一点?
编辑:
这种行为有点奇怪,但在层次结构很深的情况下,这是一个方便的快捷方式!
您可以实现以下 UITabBarControllerDelegate 方法来禁用此系统范围的快捷方式:
#pragma mark -
#pragma mark UITabBarControllerDelegate
- (BOOL)tabBarController:(UITabBarController *)tbc shouldSelectViewController:(UIViewController *)vc {
UIViewController *tbSelectedController = tbc.selectedViewController;
if ([tbSelectedController isEqual:vc]) {
return NO;
}
return YES;
}
I want to disable the default action when user taps the tabbar item.
For example, i have a tabbar with Tab1, Tab2 and Tab3. In Tab1, user can navigate from View1 to View3 (View1 > View2 > View3). If user is at View3, and he taps the Tab1, the application takes the user to View1 (the root view controller). I want to disable this functionality. I don't want the tap on Tab1 to pop all the view controllers. How can i do that?
Edit:
This behavior is a little strange, but a handy shortcut in case of deep hierarchy!
You can implement following UITabBarControllerDelegate methods to disable this system wide shortcut:
#pragma mark -
#pragma mark UITabBarControllerDelegate
- (BOOL)tabBarController:(UITabBarController *)tbc shouldSelectViewController:(UIViewController *)vc {
UIViewController *tbSelectedController = tbc.selectedViewController;
if ([tbSelectedController isEqual:vc]) {
return NO;
}
return YES;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你看一下 UITabBarController 委托,有一个方法:
如果你在你的类中实现这个方法,你可以检查 UIViewController 是否是已经显示的,然后返回 NO,这将阻止这种情况发生。
我对嵌入 UITabBarController 中的 ABPeoplePicker 对象遇到了同样的问题,因为第二次按已经显示的“联系人”选项卡将使 ABPeoplePicker 控件显示“组”
if you look at the UITabBarController delegate there is a method:
If you implement this in your class, you can check if the UIViewController is the already displayed one and then return NO, which will stop this from happening.
I had the same problem with a ABPeoplePicker object embedded in a UITabBarController, in that pressing the 'Contacts' tab a second time which was already displayed would make the ABPeoplePicker control show the 'Groups'