Showing a modal view with a navigation bar
This took me about an hour to figure out so I’m blogging it. I was interested to show a modal view with a navigation bar at the top. I wanted the navigation bar so as to put a descriptive title for the modal view. I read one solution here which is to use a label to show the title. But I kind of like the look of navigation bars at the top. Besides, the HIG from Apple shows a modal view with a navigation bar so I think showing the title this way is an accepted approach.
I added a navigation bar in the Xib file but the navigation bar was not showing in the modal view. I fiddled with some other options with no luck. Thanks to this discussion which showed me the right way.
What I did was change my original code,
DatePickerViewController *datePickerViewController = [[DatePickerViewController alloc] initWithNibName:@"DatePickerViewController" bundle:nil]; [self presentModalViewController:datePickerViewController animated:YES];
to this,
DatePickerViewController *datePickerViewController = [[DatePickerViewController alloc] initWithNibName:@"DatePickerViewController" bundle:nil]; UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:datePickerViewController]; [self presentModalViewController:navigationController animated:YES];
and my modal view has,
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Set date and time";
}
I still don’t understand why adding the navigation bar in the Xib file did not work. Maybe there is something more fundamental I am missing. I’ve spent too much time on this already though, time to move on!
Posted in: Objective C, iPhone
1 Comment
Comments RSS
TrackBack Identifier URI
Leave a comment


