[英]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. 或尝试实现
UIViewController
的viewDidLoad
或viewWillAppear
方法来设置颜色。
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.