1

I have been seeing this issue and resolving it by manually creating UINavigationController in the code. Could someone please tell me when I add Navigation Bar from XIB's Attribute Inspector -> Set Top bar to Black Navigation bar, it gets displayed in the XIB but when I run the program, it doesn't appear! I noticed that self.NavigationController was coming nil so I added UINavigationController in my XIB and assign NIB but still it's nil! What's wrong with this? Do I need any additional settings?

[EDIT1]

I tried adding it like below and it works but I want parent class to forward rotation and appearance events to child controller automatically. If I do following it won't send them because I am adding nvc as child and not marketsListViewController. So I thought I have to subclass UINavigationController. See EDIT2.

self.marketsListViewController = [[MarketsListViewController alloc] initWithNibName:@"MarketsListViewController" bundle:nil];
UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:self.marketsListViewController];
nvc.navigationBar.barStyle = UIBarStyleBlack;
[self.marketsListView addSubview:nvc.view];

[self addChildViewController:nvc];
[nvc didMoveToParentViewController:self];

[EDIT2]

I have subclassed marketsListViewController to UINavigationController and thought following will work but it doesn't. It just displays navigation bar, UITableView doesn't get displayed!

self.marketsListViewController = [[MarketsListViewController alloc] initWithNibName:@"MarketsListViewController" bundle:nil];
self.marketsListViewController.navigationBar.barStyle = UIBarStyleBlack;
[self.marketsListView addSubview:self.marketsListViewController.view];

[self addChildViewController:self.marketsListViewController];
[self.marketsListViewController didMoveToParentViewController:self];

[EDIT3]

I was wrong in Edit1 that children controller won't get rotation events when I add child as navigation controller's root controller. Parent still sends all the events automatically and that's what I want! :)

Added navigation bar from XIB

Paresh Masani
  • 7,474
  • 12
  • 73
  • 139

1 Answers1

3

Could someone please tell me when I add Navigation Bar from XIB's Attribute Inspector -> Set Top bar to Black Navigation bar, it gets displayed in the XIB but when I run the program, it doesn't appear!

What you see in you .xib file is just there to help you get your layout right and get a good idea of what it'll look like. When you have an app that uses a navigation bar, the bar is almost always managed by a navigation view controller (UINavigationController). The view controllers that the navigation controller hosts don't worry about the nav bar -- their view hierarchies go in the space beneath the bar.

So, the right way to get a navigation bar in your app is almost certainly to use a navigation controller. If you only have one view controller, make that that the navigation controller's root view controller (note: the root view controller of the window will be the navigation view controller). You can change the appearance of the bar from your view controller by setting the navigation bar's style:

self.navigationController.navigationBar.barStyle = UIBarStyleBlack;

Update: In the code you posted, you're just creating a new navigation controller, but not doing anything with it. I alluded above the the fact that (in most cases) the navigation controller should be the window's root view controller. It's fine to create your navigation controller in code, but you'll usually do it in the app delegate, and once you create it you'll set it as the window's root view controller. Here's an example borrowed from a project made fresh from Xcode's master/detail template (sans storyboards):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    MasterViewController *masterViewController = [[MasterViewController alloc] initWithNibName:@"MMMasterViewController" bundle:nil];
    self.navigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];
    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];
    return YES;
}

Note that after creating the window and the navigation controller, the code sets the nav controller as the window's root view controller.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • Thanks @Celeb for clearing my doubt! I have edited the question. Please have a look. I have many screens in container view and couple of them have navigation bar. MarketsListViewController is the view controller showed in the XIB in the question. Self represents the Container View Controller. – Paresh Masani Oct 25 '12 at 14:26
  • Hi @Celeb Unfortunately I can't do it in AppDelegate as it's not the only view controller I have. The parent(container) view controller doesn't have navigation bar. It has about 8-10 child controllers and couple of them have to have navigation bar to enable nesting views. But anyway I think I am much nearer to solution. If you see Edit1, it works but I can't user it as I want child controller to receive rotation events forwarded by parent automatically. So I have subclass my view controller to UINavigationController and done the same thing but that doesn't work!! It only shows navigationbar!! – Paresh Masani Oct 25 '12 at 14:43
  • @AppleDeveloper Sorry, I can't get a clear understanding of what you're trying to do here. Subclassing `UINavigationController` isn't something that's common -- it's probably not the right thing to do. If you haven't already, I'd strongly encourage you to read the View Controller Programming Guide. – Caleb Oct 25 '12 at 14:50
  • @Celeb sorry for the confusion. In short EDIT1 code is working absolutely fine. In that code, I have created NavigationController initialise with root view controller - marketsListViewController. So basically I have added navigation controller as a child controller to parent not marketsListViewController and hence, marketsListViewController class will not get required events! I thought alternative to this approach is subclassing navigationcontroller - EDIT2. This solution just displays navigation abr and not uitableview of my XIB in the original question. I hope I am clear. – Paresh Masani Oct 25 '12 at 14:59
  • @Celeb regarding view programming guide, I read them many times and I think I am a good programmer :) I learnt that when you trying to achieve something, you don't reinvent the wheel. Probably Apple will have already classes created for you. In this case my requirement is to have UINavigationController with navigation bar and still want my parent to forward required events to children so subclassing UINavigationController should be a good option! Isn't it? – Paresh Masani Oct 25 '12 at 15:02
  • @Celeb I figured out that why it was adding only navigatiobar when I subclass my viewcontroller to navigation controller. Basically I have to set root controller to navigation controller which is not possible to do if I subclass it. I have to create one more class that subclass uinavigationcontroller and then set it's root view controller as my child controller but that's long way! :) Edit 1 code was always working and that's what I want :) Thanks. – Paresh Masani Oct 25 '12 at 15:26