ios - 为什么 NavgationItem 引用消失了?

标签 ios objective-c uinavigationbar

我创建了一个 NavigationBar 并将其添加到 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

最佳答案

关于ios - 为什么 NavgationItem 引用消失了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49425212/

相关文章:

ios - 解析 XML - 从 URL 加载图像

ios - 滚动时隐藏导航栏而不隐藏状态栏及其背景

ios - 如何调用 id 类型的对象上的方法

ios - [self.tableview reloadData];导致闪烁

ios - 魔法记录/核心数据 : Saving Many Objects (~250K) Without Performance/Time Troubles?

ios - dispatch_sync 阻止我的 UI,我无法单击任何按钮

iOS swift : Array of Objects conforming to a Protocol: How to check if array contains

ios - 如何使用图表在 X 轴上显示每月(IOS)

ios - 当推送到详细 View Controller 时,对 UINavigationBar 内的 subview 进行动画处理

ios - 在 NavBar Swipe Hide 上,HeaderCell 创建了一个透明的间隙并且内容通过