簡體   English   中英

iOS - 在 AppDelegate 中訪問導航 Controller

[英]iOS - Access Navigation Controller in AppDelegate

我已經使用故事板構建了我的應用程序。 我有一個 UITabBarController 和一個 NavigationController 嵌入其中。 我正在嘗試使用色調 colors 自定義我的應用程序,並且我已經設法為 Tabbar 設置自定義顏色。 我能夠從 rootViewController 訪問標簽欄,但是我無法訪問導航欄。 無論如何,我可以輕松訪問它嗎? 我的 AppDelegate 代碼示例如下。

謝謝

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:  (NSDictionary *)launchOptions
{
    self.tabBarController = (UITabBarController*)self.window.rootViewController;
    UITabBar *tabBar = self.tabBarController.tabBar;
    [tabBar setSelectedImageTintColor:[UIColor greenColor]];
    tabBar.tintColor = [UIColor colorWithRed:0.4 green:0.522 blue:0.129 alpha:1];

    //This bit doesnt work
    UINavigationController *navCon = self.tabBarController.navigationController;
    navCon.navigationBar.tintColor = [UIColor colorWithRed:0.384 green:0.259 blue:0.161     alpha:1];

    return YES;    
}

如果您使用的是導航控制器,則希望它們嵌入由選項卡欄 controller 管理的視圖控制器。

試試NSLog(@"%@", [self.tabBarController viewControllers]); 並查看它是否列出了導航控制器或您的自定義 controller。例如,我創建了兩個由我的選項卡欄觸發的視圖控制器,一個有導航,一個沒有導航,上面的行顯示:

 (
     "<UINavigationController: 0x6a35f20>",
     "<SecondViewController: 0x6a39500>" )

所以,我可以得到我的[[self.tabBarController viewControllers] objectAtIndex:0]

假設您沒有使用 IBOutlet,我建議您可以將它們與 XIB 文件一起使用。 例如,在 header 文件中:

IBOutlet UINavigationBar *aNavBar;

然后在.m中,切換:

UINavigationController *navCon = self.tabBarController.navigationController;
navCon.navigationBar.tintColor = [UIColor colorWithRed:0.384 green:0.259 blue:0.161     alpha:1];

和:

[aNavBar setTintColor:[UIColor colorWithRed:0.384 green:0.259 blue:0.161     alpha:1]];

還有你 go,我做的很快。 所以告訴我它是否有效。
-賈斯汀 A。

暫無
暫無

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

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