How do i add add a navigation controller to a xib
You don't need to set a UINavigationController
in a xib file the same way you would with storyboards, as a xib only represents one screen. The normal thing to would be alloc/init a UINavigationController
, and a UIViewController
, and set the view controller as the root of the navigation controller before you display it. This can be done in the App Delegate if you want you whole app wrapped in a navigation controller form the start.
In the app delegate in a non-storyboard app you would want something like this
self.navigation = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]];
[self.window makeKeyAndVisible];
self.window.rootViewController = self.navigation;
Where self.navigation
is a UINavigationController
property or iVar belonging to the App Delegate. If you want to set up the view controller properly in the xib, select the root view in the xib and open the attributes inspector (RHS, the one that looks like a small shield) and choose 'Navigation Bar' in the 'Top Bar' drop down menu. This will display a navigation controller top bar in xib so you can position the other views accordingly.
EDIT
That's not entirely correct, you can create the navigation controller in a xib file, but I've always considered it more hassle than it worth, as there is very few visual elements to it, and they can be set in code very easily.
More info in this tutorial here
http://www.iosdevnotes.com/2011/03/uinavigationcontroller-tutorial/
JSA986
Updated on June 07, 2022Comments
-
JSA986 almost 2 years
Having only used storyords to subclass a navigation controller before I cant find any tutorial on how to do this for xibs.
How do I add a naviagtion controller to a certain xib so I can subclass it? Then I can override the rotation for my view. The part im stuck on is how does the view controller know about the navigation controller? In storyboards I would manually hook it up to my view controller. I just cant work it out with xibs?
Ive added a navigation controller next to my views xib...now I need some advice from here.
Also I've created iPad views from my original iPhone xibs. When I drag the nav controller from IB its still in the iPhone screen size?
-
JSA986 over 11 yearsThanks for answer, makes sense. Ive actually already got my navigation set in my app delegate, im trying to override the iOS 6 autorotation for just a few of the views. Normally id just subclass a nav controller with my methods in storyboards but unsure of this with xibs.
-
JSA986 over 11 yearsThats actually what im trying to achieve, In storyboards id just add a nav controller and subclass it with my methods for handling the rotation in ios 5 & 6, however using xibs I cant figure out how to do it? Whats a P- double code? My nav controller code is in my delegate, i just need to override a few screens, iOS 6 screen rotation has thrown a spanner ion the works!
-
RyanMullins over 11 yearsJust as a side note, it's subclassing UINavigationController is typically discouraged. Discussion on this topic here.
-
Guy Kogus over 11 yearsIn the storyboard you can change the class of the navigation controller. Select it and in the Utilities editor (on the right) select the Identity Inspector (cmd+alt+3) and you can change the class type to your custom class.
-
JSA986 over 11 yearsBut the whole point of my question is im NOT using storyboards and thats the problem, I dont know how to achieve this when using xibs
-
JSA986 over 11 years@ Ryan, fair point, so how do I override rotation without subclassing nav controller with own methods in iOS 6 as NOTHING seems to work without doing that for me?
-
Guy Kogus over 11 yearsIt works the same way. Open your main window's XIB or whichever XIB contains the navigation controller, and set the class there.
-
LKM about 9 yearsAnd then button event doesn't make it