iPhone,UITabBarItem:应用程序启动时动态更改选项卡栏的图像

发布于 2024-10-04 18:53:20 字数 1462 浏览 0 评论 0原文

当我的应用程序加载时,我想根据用户上次运行应用程序时设置的保存设置更改其中一个选项卡上的图像。当用户单击执行该选项卡的视图控制器的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遗忘曾经 2024-10-11 18:53:20

我想通了。我只需要调用 UITabBarItem 的 setImage 方法。

//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 分配给指针然后设置图像是否有原因或优点?

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

还是用一行代码来完成,如下所示?

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

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"]];
}

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"]];
海未深 2024-10-11 18:53:20

我认为你需要尝试这个,希望这会有所帮助,

我已经更改了选定的 tabbatitem 图像,例如 -

在选项卡控制器委托方法中,

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController

{
    if([tabBarController selectedIndex] == 0)
    {
        [viewController.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"selected.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"unselect.png"]];
    }    
}

通过此你可以更改你的图像。

或者您可以直接在视图控制器 init(或 ViewWillAppear) 方法中使用,例如

        [viewController.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"selected.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"unselect.png"]];

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

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController

{
    if([tabBarController selectedIndex] == 0)
    {
        [viewController.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"selected.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"unselect.png"]];
    }    
}

through this you can change your image.

Or you can use directly in your view controllers init(or ViewWillAppear) method, like

        [viewController.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"selected.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"unselect.png"]];

Try it!!!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文