Crystal
Crystal

Reputation: 29448

UINavigationController in a UIPopoverController, display title not working

I'm not sure why this isn't working as it is working in another area of my app. I am trying to display the title for the navigationController that is the contentViewController of the popover. This is the code:

        DetailView* details = [[[DetailView alloc] initWithTarget:target] autorelease];
        UINavigationController* content = [[[UINavigationController alloc] initWithRootViewController:details] autorelease];
        self.popover = [[[UIPopoverController alloc] initWithContentViewController:content] autorelease];
        [self.popover setDelegate:self];
    }    
    [self.popover presentPopoverFromRect:frame inView:self.scrollView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

Than in my DetailView.m:

- (id)initWithTarget:(Target*)aTarget
{
    self = [super initWithNibName:@"TargetDetailView" bundle:nil];
    if (self) {
        // Custom initialization
        target = [aTarget retain];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"Target";
}

I have also tried

content.navigationItem.title = @"Target";

but that does not give me a title in my popover. Any thoughts? TIA.

Upvotes: 1

Views: 1687

Answers (2)

Mark Adams
Mark Adams

Reputation: 30846

All you have to do is set the title property of a UIViewController. Once this controller gets picked up into a navigation hierarchy, the UINavigationController will read this title into it's own navigation bar. So instead of putting in viewWillAppear: to work around the navigation controller, just set it in -viewDidLoad and everything will be handled automatically.

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"Target";
}

Upvotes: 0

Michael Frederick
Michael Frederick

Reputation: 16714

put this in viewWillAppear:animated

self.navigationItem.title = @"Target";

I suspect that viewDidLoad is getting called on the DetailView before it is a part of the UINavigationController and therefore it might not have a navigationItem at that time.

Upvotes: 2

Related Questions