OGeek|极客世界-中国程序员成长平台

标题: iphone - 如何更改 TabBarItem 图像和标题问题 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 16:33
标题: iphone - 如何更改 TabBarItem 图像和标题问题

我正在使用带有导航 Controller 的基于 tabBar 的应用程序。在我的应用程序中,我想更改 tabBarItem imagetitle。在单击特定的 tabBarItem 时,控件转到该特定 View 的 viewWillAppear 方法。这次我想更改那个特定 tabBarItemimagetitle

我是这样给的,但它不起作用......

代码:

tabItem = [[UITabBarItem alloc] initWithTitle"Colors" image:[UIImage imageNamed"Colors.png"] tag:9];

self.tabBarController.tabBarItem = tabItem;

我已经为那个 View 设置了 tag value9

请帮助我如何更改该特定 tabbarItemimagetitle



Best Answer-推荐答案


您尝试做的事情有一点误解。首先, UITabBarController 没有 tabBarItem 属性,这是您试图设置的。但是,即使确实如此,UITabBarController 文档也明确指出,关于 UITabBarController 的 tabBar 属性:

You should never attempt to manipulate the UITabBar object itself stored in this property. If you attempt to do so, the tab bar view throws an exception.

所以,修改item的方式不是通过UITabBar本身。在您的情况下, UITabBar 从每个选项卡表示的 UIViewControllers 获取其 UITabBarItems 。这是你可以控制的。具体来说,你可以做的是:

- (void)viewWillAppearBOOL)animated {
    /* Modifies the UITabBarItem used by a UITabBarController to represent this
     * UIViewController in the tab bar.
     */

    UITabBarItem* tabBarItem =  [[UITabBarItem alloc] initWithTitle"Colors" image:[UIImage imageNamed"Colors.png"] tag:9];
    self.tabBarItem = tabBarItem;
    [tabBarItem release];
}

关于iphone - 如何更改 TabBarItem 图像和标题问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4721669/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4