我有一个 UITabBarController
,它链接到 5 个 View Controller ,如下图所示。
在我的打开屏幕中,我有注册页面,如果用户没有注册,那么我不想让用户看到 Rewards 和 Offers ViewController 的任何内容。
每当用户点击 tabbarviewcontroller 时,我想生成一个显示注册或取消的 UIAlertView
。
我的问题是,当用户点击 Rewards 或 Offers 时,我应该如何知道点击了哪个标签栏菜单来生成 UIAlertView
?
我正在使用 Storyboard。我有一个 tabbarcontroller
和这个 tabbarcontroller 的自定义类。头文件如下:
#import <UIKit/UIKit.h>
@interface MainHarvestGrillTabBarViewController : UITabBarController <UITabBarDelegate>
@end
我想知道我应该如何访问单击的选项卡。
我已经实现了下面的代码,但是无论点击哪个标签,它总是返回o。
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
NSUInteger index = self.tabBarController.selectedIndex;
NSLog(@"index %lu",(unsigned long)index);
// Recenter the slider (this application does not accumulate multiple filters)
// Redraw the view with the new settings
}
最佳答案
您可以使用选项卡栏的 didSelectItem
检查选择了哪个选项卡协议(protocol)方法,但这不会阻止显示 View Controller 。相反,您可以使用 UITabBarControllerDelegate
协议(protocol)并实现 shouldSelectViewController:(UIViewController *)viewController
方法。因此,您将检查所选的 View Controller 并采取相应的行动。以下是防止用户选择“奖励”选项卡的示例:
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
if ([tabBarController.viewControllers objectAtIndex:3] == viewController) {
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Alert message" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
[alertView show];
return NO;
}
return YES;
}
您可以将它放在您的选项卡栏 Controller 类中。您还需要添加 <UITabBarControllerDelegate>
到您的头文件并设置 self.delegate = self;
在 viewDidLoad
.
关于ios - 如何知道点击了哪个 Tabbar 菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23139606/