简体   繁体   English

iOS - 在 AppDelegate 中访问导航 Controller

[英]iOS - Access Navigation Controller in AppDelegate

I have built my app using Storyboards.我已经使用故事板构建了我的应用程序。 I have a UITabBarController and a NavigationController embedded it that.我有一个 UITabBarController 和一个 NavigationController 嵌入其中。 I am trying to customise my app with tint colors and I have managed to set a custom color for the Tabbar.我正在尝试使用色调 colors 自定义我的应用程序,并且我已经设法为 Tabbar 设置自定义颜色。 I was able to access the tabbar from the rootViewController, however I cannot get access to the navigation bar.我能够从 rootViewController 访问标签栏,但是我无法访问导航栏。 Is there anyway that I can easily access this?无论如何,我可以轻松访问它吗? Sample of my AppDelegate code is below.我的 AppDelegate 代码示例如下。

Thanks谢谢

- (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;    
}

If you're using navigation controllers, would expect them to embed the view controllers that are managed by the tab bar controller.如果您使用的是导航控制器,则希望它们嵌入由选项卡栏 controller 管理的视图控制器。

Try NSLog(@"%@", [self.tabBarController viewControllers]);试试NSLog(@"%@", [self.tabBarController viewControllers]); and see whether it lists navigation controllers or your custom controller. For example, I created two view controllers that are triggered by my tab bar, one with and one without navigation and the above line shows:并查看它是否列出了导航控制器或您的自定义 controller。例如,我创建了两个由我的选项卡栏触发的视图控制器,一个有导航,一个没有导航,上面的行显示:

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

So, I could get mine as [[self.tabBarController viewControllers] objectAtIndex:0] .所以,我可以得到我的[[self.tabBarController viewControllers] objectAtIndex:0]

Assuming you did no work with IBOutlet's, I suggest you maybe use them along with the XIB file.假设您没有使用 IBOutlet,我建议您可以将它们与 XIB 文件一起使用。 For example, In the header file:例如,在 header 文件中:

IBOutlet UINavigationBar *aNavBar;

And then in the.m, switch:然后在.m中,切换:

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

With:和:

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

And there you go, I made this quick.还有你 go,我做的很快。 so tell me if it works or not.所以告诉我它是否有效。
- Justin A. -贾斯汀 A。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM