簡體   English   中英

在UITabBarController中設置UINavigationController的UITabBarItem

[英]Set UINavigationController's UITabBarItem inside a UITabBarController

我正在開發一個iphone應用程序,它在每個選項卡中都有一個帶有UINavigationControllers的主視圖控制器(UITabBarController)。 問題是我無法更改TabBarItem的數據(標題和圖像)。

這是我加載viewcontrollers的方法:

AroundViewController *aroundViewController = [[AroundViewController alloc] init];

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:aroundViewController];
[[navController navigationBar] setTintColor:[UIColor grayColor]];

UITabBarController *tabBarController = [[UITabBarController alloc] init];

NSArray *viewControllers = [NSArray arrayWithObjects:navController, nil];
[tabBarController setViewControllers:viewControllers];

[self presentViewController:tabBarController animated:YES completion:nil];

我正在嘗試在aroundviewcontroller的init中設置tabbaritem,但我沒有成功。 我正在閱讀,一個解決方案是,在初始化,

[self setTitle:@"whatever"]; 

這工作但我無法改變圖像。

提前致謝。

使用視圖控制器的tabBarItem實例直接訪問選項卡欄項!

[self.tabBarItem ....]

[self.tabBarItem setImage: ...]
[self.tabBarItem setTitle: ...]

編輯:

hm,然后在init或viewWillAppear方法中嘗試以下操作:

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

在為UITabBarController設置UIViewControllers之前,您可以這樣做:

[navController.tabBarItem setImage:[UIImage imageNamed:@"MY-IMAGE"]];
[navController setTitle:@"MY-TITLE"];

NSArray *viewControllers = [NSArray arrayWithObjects:navController, nil];
[tabBarController setViewControllers:viewControllers];

我找到了解決方案。 在viewWillAppear中我放了:

[[[self navigationController] tabBarItem] setTitle:@"HELLO"];

這樣可行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM