iOS - 检查选择了哪个UITabBar选项的简单方法

时间:2014-05-09 23:47:39

标签: ios if-statement uitabbaritem nsindexpath

我的目标是创建一个if / else语句,以便我可以说:如果选择了TabBarItem,则将setSelectedImageTintColor设置为此颜色。

我是ObjC的新手,并不完全确定如何实现if语句。这是我的viewDidLoad:

- (void)viewDidLoad
{
[super viewDidLoad];

UITabBarItem *item0 = [self.tabBar.items objectAtIndex:0];
item0.image = [[UIImage imageNamed:@"red.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];


UITabBarItem *item1 = [self.tabBar.items objectAtIndex:1];
item1.image = [[UIImage imageNamed:@"yellow.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

UITabBarItem *item2 = [self.tabBar.items objectAtIndex:2];
item2.image = [[UIImage imageNamed:@"green.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

UITabBarItem *item3 = [self.tabBar.items objectAtIndex:3];
item3.image = [[UIImage imageNamed:@"black.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

[[UITabBar appearance] setSelectionIndicatorImage:
 [UIImage imageNamed:@"item.png"]];


}

正如你所看到的,我为每个TabBarItem指定了一个单独的图像,我想使高亮颜色与图像匹配(所以red.png的红色高亮,yellow.png的黄色等)

如何实现if / else语句?同样,我想检查indexPath(0-3),然后为tabBarItem设置自定义setSelectedImageTintColor。对我来说,另一个选择就是完全删除重点,如果这样更实用的话。

3 个答案:

答案 0 :(得分:1)

- (void)viewDidLoad
{
[super viewDidLoad];

UITabBarItem *item0 = [self.tabBar.items objectAtIndex:0];
item0.image = [[UIImage imageNamed:@"red.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];


UITabBarItem *item1 = [self.tabBar.items objectAtIndex:1];
item1.image = [[UIImage imageNamed:@"yellow.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

UITabBarItem *item2 = [self.tabBar.items objectAtIndex:2];
item2.image = [[UIImage imageNamed:@"green.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

UITabBarItem *item3 = [self.tabBar.items objectAtIndex:3];
item3.image = [[UIImage imageNamed:@"black.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

[[UITabBar appearance] setSelectionIndicatorImage:
 self.tabBar.selectedItem.image];


}

如果您希望在切换标签时更新图像:

在你的.h文件中,你会有这样的一行:

@interface MyClass : UIViewController

将其更改为

@interface MyClass : UIViewController <UITabBarDelegate>

当然,用你的班级名称替换MyClass。

viewDidLoad:中,添加以下行:

self.tabBar.delegate = self;

在.m文件中,实现tabBar:didSelectItem:,如下所示:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    [[UITabBar appearance] setSelectionIndicatorImage:item.image];
}

答案 1 :(得分:0)

我并不是100%肯定你在上面尝试做什么(包括截图会很好),但是如果你想知道选择了哪个UITabBar项目,你应该在UITabBarController上设置一个委托,然后编写一个实现的方法:

-tabBarController:didSelectViewController:

然后您可以更改标签栏项目的颜色或图标。

如果您将标签栏控制器设置为IBOutlet,您可以通过查看控制器"selectedIndex" property以编程方式询问选择了哪个标签。

答案 2 :(得分:0)

您应该将UITabBarDelegate protocol实现到ViewController中,并将UITabBar委托设置为自我

self.tabBar.delegate = self;

之后,您可以实施方法tabBar:didSelectItem:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    // Check here for the item and change tintColor accordingly
    // For example:
    if([item isEqual:[self.tabBar.items objectAtIndex:1]) {
        tabBar.selectedImageTintColor = [UIColor redColor];
    }
}