iPhone,UITabBarItem:应用程序启动时动态更改选项卡栏的图像
当我的应用程序加载时,我想根据用户上次运行应用程序时设置的保存设置更改其中一个选项卡上的图像。当用户单击执行该选项卡的视图控制器的 ViewDidLoad 方法的选项卡时,我可以更改图像。见下文:
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 方法。
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,但没有任何效果。我尝试在 UITabBarItem 和 UINavigationController 的根视图控制器中创建 IBOutLets。
// tb is an iboutlet to the UITabBarItem
self.tb = tabItem;
// nc is an iboutlet to the UINavigationController
self.nc.tabBarItem = tabItem;
无济于事。知道如何做到这一点吗?
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];
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];
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想通了。我只需要调用 UITabBarItem 的 setImage 方法。
后续问题:首先将 tabbaritem 分配给指针然后设置图像是否有原因或优点?
还是用一行代码来完成,如下所示?
I figured it out. I just needed to call the setImage method of the UITabBarItem.
Follow up question: is there a reason or advantage to assigning the tabbaritem to a pointer first and then setting the image?
versus doing it in one line of code as follows?
我认为你需要尝试这个,希望这会有所帮助,
我已经更改了选定的 tabbatitem 图像,例如 -
在选项卡控制器委托方法中,
通过此你可以更改你的图像。
或者您可以直接在视图控制器 init(或 ViewWillAppear) 方法中使用,例如
Try it!!!!
I think you need to try this one, hope this will help,
I have change the selected tabbatitem image like -
in tabbar controller delegate method
through this you can change your image.
Or you can use directly in your view controllers init(or ViewWillAppear) method, like
Try it!!!!