hej
hej

Reputation: 16

Why does NavgationItem reference disappears?

I created a NavigationBar and added it to the UIViewController. But after init, the reference turns to nil. I'm new to iOS and OC, I don't know why. Anyone can help? Thank you.

code summary:

@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

Upvotes: 0

Views: 29

Answers (1)

Naren Krish
Naren Krish

Reputation: 269

This will be useful for you, kindly check and do Tutorial point site is very easy to learn some important UI basics if you are working in Objective C

Upvotes: 1

Related Questions