简体   繁体   English

如何从UIViewController访问UINavigationController

[英]How to access UINavigationController from a UIViewController

In my AppDelegate I have the following code: 在我的AppDelegate中,我有以下代码:

UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
UINavigationController *itemsNavigationController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];

How can I set the UIVNagigationBar's background color to green? 如何将UIVNagigationBar的背景颜色设置为绿色?

If I do: 如果我做:

[self.navigationController.navigationBar setTintColor:[UIColor greenColor]];

from the viewController1, this has no effect. 从viewController1,这无效。

Thanks in advance! 提前致谢!

UPDATE: 更新:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"MyTitle", @"");

        [self.navigationController.navigationBar setTintColor:[UIColor greenColor]];
        [self.parentViewController.navigationController.navigationBar setTintColor:[UIColor greenColor]];
    }
    return self;
}

Both have no effects 两者都没有作用

In the initWithNibName:bundle method your view controller can't yet have a navigation controller. initWithNibName:bundle方法中,您的视图控制器尚不能使用导航控制器。

If your navigation controller is created from a nib file too, try implementing the awakeFromNib method of UIViewController and set the color there. 如果导航控制器也是从nib文件创建的,请尝试实现UIViewController的awakeFromNib方法并在其中设置颜色。 awakeFromNib is called once every object on the nib file has been loaded and initialized. 一旦加载并初始化了nib文件上的每个对象, awakeFromNib调用awakeFromNib

If you create the navigation controller programmatically, then you should set the color only after adding the view controller (loaded from the nib) to your navigation controller. 如果以编程方式创建导航控制器,则仅应在将视图控制器(从笔尖加载)添加到导航控制器中之后,才设置颜色。 You can either do this where you add the view controller. 您可以在添加视图控制器的地方执行此操作。 Or try implementing the viewDidLoad or viewWillAppear methods of UIViewController to set the color. 或尝试实现UIViewControllerviewDidLoadviewWillAppear方法来设置颜色。

You're doing it too soon. 您做得太早了。 (You're not getting an error, but your messages are fruitless messages sent to nil , as you can easily discover by using NSLog.) Wait until FirstViewController's viewDidLoad . (您没有收到错误,但是您的消息是发送到nil无效消息,因为您可以使用NSLog轻松找到它。)等待FirstViewController的viewDidLoad

Or use the appearance proxy as already suggested. 或使用已建议的外观代理。 The advantage here is partly that timing becomes unimportant; 这里的优势部分是时间变得无关紧要。 you can do it in advance, before any view controller instances even exist. 您可以在任何视图控制器实例还不存在之前就提前完成。 Example: 例:

https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/ch19p549navigationInterface/p471p482navigationInterface/AppDelegate.m https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/ch19p549navigationInterface/p471p482navigationInterface/AppDelegate.m

You can also use the appearance method in iOS 5 to change tints for your navigation bars across the app. 您还可以使用iOS 5中的appearance方法来更改应用程序中导航栏的色彩。 For example, in your application:didFinishLaunchingWithOptions you can: 例如,在您的application:didFinishLaunchingWithOptions您可以:

if ([[UINavigationBar class] respondsToSelector:@selector(appearance)]) 
{
    [[UINavigationBar appearance] setTintColor:[UIColor greenColor]];
}

尝试[itemsNavigationController1.navigationBar setTintColor:[UIColor greenColor]];

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

相关问题 如何从 Uiviewcontroller 加载 UINavigationController - how to load UINavigationController from Uiviewcontroller UIViewController中的UINavigationController - UINavigationController from UIViewController 从 uinavigationcontroller 中查找 uiviewcontroller - Find a uiviewcontroller from uinavigationcontroller 从UIViewController加载UINavigationController作为子视图 - Load a UINavigationController as a subview from UIViewController 如何从已经在UINavigationController堆栈上的UIViewController调用方法 - How to call a method from a UIViewController thats already on the UINavigationController stack 如何从UINavigationController弹出UIViewController之前显示警报 - How to show alert before UIViewController is popped from UINavigationController 如何用替换的 UINavigationController 推送 UIViewController - How to push UIViewController with replaced UINavigationController 如何将UIViewController从使用此视图的不同UIViewController的单独UIView推入UINavigationController - How to push UIViewController into UINavigationController from seperate UIView of different UIViewController which uses this view 从UIViewController切换到UINavigationController丢失选项卡栏 - switch from UIViewController to UINavigationController losing tab bar iOS-从UINavigationController中以全屏显示UIViewController - iOS - Display a UIViewController as fullscreen from within a UINavigationController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM