为什么NavgationItem引用会消失?

时间:2018-03-22 09:41:53

标签: ios objective-c uinavigationbar

我创建了一个NavigationBar并将其添加到UIViewController中。但是在init之后,引用变为零。我是iOS和OC的新手,我不知道为什么。有人可以帮忙吗?谢谢。

代码摘要:

@interface ContainerViewController() 
@property (nonatomic, retain) UINavigationBar *nav;
@property (nonatomic, retain) UINavigationItem *navItem;
@end

@implementation ContainerViewController

- (instancetype) initWithParams:(NSDictionary *)params {
    self = [super init];
    if (self) {//...}
    return self;
}

- setNavTitle:(NSDictionary *) params {
    NSString *title = params[@"title"];
    /////////////////////////////////
    // here goes wrong
    // self.navItem == nil here, why?
    /////////////////////////////////
    self.navItem.title = title;
}

- (void) viewWillAppear:(Bool)animated {
    [super viewWillAppear:NO];

    static float navHeight         = 64.0;
    UIViewController *wvController = [WebView init here];
    UINavigationBar *nav           = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), navHeight)];

    UINavigationItem *navItem    = [[UINavigationItem alloc] initWithTitle:title];
    nav.items = [NSArray arrayWithObjects: navItem, nil];

    ///////////////////////////////
    // I saved the reference here
    //////////////////////////////

    [self setNav:nav];
    [self setNavItem:navItem];

    [self.view addSubview:nav];
    [self addChildViewController:wvController];
    wvController.view.bounds = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - navHeight);
    [self.view addSubview:wvController.view];
    [wvController didMoveToParentViewController:self];
}

@end

1 个答案:

答案 0 :(得分:1)

相关问题