Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 为什么NavgationItem引用会消失?_Ios_Objective C_Uinavigationbar - Fatal编程技术网

Ios 为什么NavgationItem引用会消失?

Ios 为什么NavgationItem引用会消失?,ios,objective-c,uinavigationbar,Ios,Objective C,Uinavigationbar,我创建了一个导航栏并将其添加到UIViewController中。但是在init之后,引用变为nil。我是iOS和OC的新手,我不知道为什么。有人能帮忙吗?多谢各位 代码摘要: @interface ContainerViewController() @property (nonatomic, retain) UINavigationBar *nav; @property (nonatomic, retain) UINavigationItem *navItem; @end @impleme

我创建了一个导航栏并将其添加到UIViewController中。但是在init之后,引用变为nil。我是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

这将对您有用,请检查并执行

我认为您的声明应该在viewdidload中执行,因为viewdidload将出现执行多次,而viewdidload仅在W时间执行。请尝试viewdidload而不是ViewWillLoad。我尝试过了,它不起作用。