Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i add add a navigation controller to a xib

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?

like image 471
JSA986 Avatar asked Dec 18 '12 11:12

JSA986


People also ask

How do I add navigation controller to XIB?

You can add it programmatically instead. For example: // Swift let navController = UINavigationController(rootViewController: Your View Controller) // Obj-C UINavigationController *navControler = [[UINavigationController alloc] initWithRootViewController: YourViewController];

Which is better XIB or storyboard?

It is not to tell which one is the best. because which one is good to tell based on team requirement. If you are a single developer, it is good to use storyboard because it consumes less time. If the team consists of many developers, use xib, otherwise, it is not easy to merge the modules/tasks.


1 Answers

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/

like image 109
Sam Clewlow Avatar answered Nov 06 '22 21:11

Sam Clewlow