ios - 如何知道点击了哪个 Tabbar 菜单

标签 ios tabbar

我有一个 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
}

enter image description here

最佳答案

您可以使用选项卡栏的 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/

相关文章:

ios - 在 Xcode 中使用自动化工具检测 HTTP 响应

iphone - 如何更改 UINavigationBar 的背景?

swiftui - 如何在 iOS 15 中使用 SwiftUI 在特定 View 中隐藏 TabBar

ios - 您的账户已经有一个有效的 iOS Distribution certificate 证书

ios - 为什么要在版本控制下包含 Podfile.lock?

ios - 基于背景图像的文本颜色

ios - 是否可以删除 Tabbar 背景和框?

iphone - 检查 tabBar 在 iOS 应用程序上是否可见

ios - 从 Storyboard更改选项卡栏按钮序列

ios - 夏令时月末日期问题