iPhone,UITabBarItem:在应用启动时动态更改标签栏的图像 [英] iPhone, UITabBarItem: Dynamically change image of a tab bar when app starts

查看:122
本文介绍了iPhone,UITabBarItem:在应用启动时动态更改标签栏的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的应用加载时,我想根据用户上次运行应用时设置的保存设置更改其中一个标签上的图像。当用户点击执行该选项卡的viewcontroller的ViewDidLoad方法的选项卡时,我能够更改图像。见下文:

When my app loads I want to change the image on one of the tabs based on a saved setting set by the user the last time they ran the app. I am able to change the image when the user clicks on the tab which executes that tab's viewcontroller's ViewDidLoad method. see below:

UITabBarItem *tabItem;
if (condition = YES) { 
    tabItem = [[UITabBarItem alloc] initWithTitle:@"Filter" image:[UIImage imageNamed:@"filter plus.png"] tag:self.view.tag];
}
else {
    tabItem = [[UITabBarItem alloc] initWithTitle:@"Filter" image:[UIImage imageNamed:@"filter.png"] tag:self.view.tag];
}
self.navigationController.tabBarItem = tabItem;
[tabItem release];
[super viewDidLoad]; 

但我一直无法弄清楚如何访问和更改根目录中该选项卡的UITabBarItem在加载时查看应用程序的控制器。请参阅下面的根视图控制器的viewdidload方法。

But I have been unable to figure out how to access and change the UITabBarItem of that tab in the root view controller of the app when it loads. See viewdidload method of root view controller below.

UITabBarItem *tabItem;
if (condition = YES) { 
    tabItem = [[UITabBarItem alloc] initWithTitle:@"Filter" image:[UIImage imageNamed:@"filter plus.png"] tag:self.view.tag];
}
else {
    tabItem = [[UITabBarItem alloc] initWithTitle:@"Filter" image:[UIImage imageNamed:@"filter.png"] tag:self.view.tag];
}
// get the view controller of the tab I want to change
MyViewController *vc = [self.tabBarController.viewControllers objectAtIndex:2];
ft.tabBarItem = tabItem;
[tabItem release];
[super viewDidLoad]; 

当这不起作用时,我尝试了多种其他方式来访问和更改uitabbaritem,但没有任何效果。我尝试在根视图控制器中创建IBOutLets到UITabBarItem和UINavigationController。

When this did not work I tried multiple other ways to access and change the uitabbaritem but nothing worked. I tried creating IBOutLets in the root view controller to UITabBarItem and UINavigationController.

// tb is an iboutlet to the UITabBarItem
self.tb = tabItem;  

// nc is an iboutlet to the UINavigationController
self.nc.tabBarItem = tabItem;   

无济于事。知道怎么做吗?

to no avail. Any idea how to do this?

推荐答案

我弄清楚了。我只需要调用UITabBarItem的setImage方法。

I figured it out. I just needed to call the setImage method of the UITabBarItem.

//UITabBarItem *filterTab = [self.tabBar.items objectAtIndex:2];
if (condition == YES) { 
    [[self.tabBar.items objectAtIndex:2] setImage:[UIImage imageNamed:@"filter plus.png"]];
    // [filterTab setImage:[UIImage imageNamed:@"filter plus.png"]];        
}
else {
    [[self.tabBar.items objectAtIndex:2] setImage:[UIImage imageNamed:@"filter.png"]];
    //[filterTab setImage:[UIImage imageNamed:@"filter.png"]];
}

跟进问题:是否有理由或优势将tabbaritem分配给a首先指针然后设置图像?

Follow up question: is there a reason or advantage to assigning the tabbaritem to a pointer first and then setting the image?

UITabBarItem *filterTab = [self.tabBar.items objectAtIndex:2];
[filterTab setImage:[UIImage imageNamed:@"filter plus.png"]];

与在一行代码中执行如下操作?

versus doing it in one line of code as follows?

[[self.tabBar.items objectAtIndex:2] setImage:[UIImage imageNamed:@"filter.png"]];

这篇关于iPhone,UITabBarItem:在应用启动时动态更改标签栏的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆