NIB中的导航项始终为零

时间:2013-06-05 17:58:24

标签: ios cocoa-touch uinavigationcontroller uinavigationbar nib

我在视图控制器NIB中设置了UINavigationBar,并为该栏及其关联的UINavigationItem设置了出口。在视图控制器的viewDidAppear:中,我调试了对象。导航栏设置为对象,但导航项为nil。导航栏的items数组包含一个UINavigationItem,指向有效对象。

为什么NIB中的导航项目没有设置?

enter image description here

这是我在视图控制器中使用的代码:

@interface NKViewController ()
{
    IBOutlet UINavigationBar *navigationBar;
    IBOutlet UINavigationItem *navigationItem;
}
@end

@implementation NKViewController

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    UINavigationItem *itemsNavigationItem = [[navigationBar items] objectAtIndex:0];
    NSLog(@"Items Navigation Item: %@", itemsNavigationItem);
    NSLog(@"ivar Navigation Item: %@", navigationItem);
}
@end

哪些日志:

Items Navigation Item: <UINavigationItem: 0x71a0fb0>
ivar Navigation Item: (null)

1 个答案:

答案 0 :(得分:2)

navigationItem已经(已经)UIViewController上的(只读)属性(由_navigationItem ivar备份),可能是导致此问题的原因。尝试重命名该属性,它应该没问题(实际上在我的Xcode版本中,如果我甚至尝试将其添加为插座,则会引发错误)

enter image description here

更新:已经使用其他名称进行了测试,看起来确实没问题

enter image description here

更新更新:这是ivar的名称

@interface ViewController ()
{
    IBOutlet UINavigationBar *navigationBar;
    IBOutlet UINavigationItem *navigationItemX;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    UINavigationItem *itemsNavigationItem = [[navigationBar items] objectAtIndex:0];
    NSLog(@"Items Navigation Item: %@", itemsNavigationItem);
    NSLog(@"ivar Navigation Item: %@", navigationItemX);

}

哪些日志:

Items Navigation Item: <UINavigationItem: 0x71b11c0>
ivar Navigation Item: <UINavigationItem: 0x71b11c0>

最后,通过这样做我们可以看到navigationItem确实是另一回事:

NSLog(@"Items Navigation Item: %@", itemsNavigationItem);
NSLog(@"ivar Navigation Item: %@", navigationItemX);
NSLog(@"'Hidden' ivar: %@", [self valueForKey:@"navigationItem"]); // or self.navigationItem

哪些日志:

Items Navigation Item: <UINavigationItem: 0x7185190>
ivar Navigation Item: <UINavigationItem: 0x7185190>
'Hidden' ivar: <UINavigationItem: 0x759e040>