Loading UINavigationController from another nib automatically by UITabBarController

大憨熊 提交于 2019-12-20 14:44:24

问题


I think I've found the cause: Document Info window in IB has a warning: "'Selected Navigation Controller (Second)' has nib name property set to 'SecondView.nib', but this view controller is not intended to have its view set in this manner."

Bummer.


I've built nib in Interface Builder that has UITabBarController at top level and switches between UINavigationControllers.

It works fine when everything is in a single nib file, but I'd like to use separate nib files for UINavigationControllers.

Starting with Apple's TabBar template, if I just change class of SecondView to UINavigationController, it all breaks:

and all I get is this:

// imgur has lost the image, sorry //

Is it possible to have separate file for UINavigationController without programmatically setting everything?

I would like TabBarController to handle loading and unloading of nibs.


回答1:


Simply swap the UINavigationController with the FirstViewController.

So the hierarchy should be like this:

Tab bar controller
-----Tab bar
-----Navigation Controller
----------First View Controller
---------------Navigation Item
----------Tab bar item (First)
-----Navigation Controller
----------Second View Controller
---------------Navigation Item
----------Tab bar item (Second)

You set the nib of First View Controller in the inspector to the nib file containing the actual view objects (Since you are trying to split them into separate files, which is a good thing).

You have one tab, that tab has a navigation controller which loads First View Controller as its root view.

Done.




回答2:


I haven't tried setting up UINavigationController via IB. I have multiple screens, each is stored in separate xib and there's a corresponding class that extends UIViewController. In applicationDidFinishLaunching I initialize UIViewControllers using xib's but then manually create UINavigationController, add navigation controller's view to window and push first view to navigation controller.

Not sure if that helps.

- (void)applicationDidFinishLaunching:(UIApplication *)application {    

    navigationController = [[UINavigationController alloc] init];

    FirstViewController * viewController = [[FirstViewController alloc] 
                                           initWithNibName:@"FirstView"
                                                    bundle:nil];
    [navigationController pushViewController:viewController animated:NO];
    [viewController release];
    [window addSubview:navigationController.view];
    [window makeKeyAndVisible];
}

Above FirstViewController extends UIViewController, in IB you create your view then set File's owner class to your class (e.g. here FirstViewController) and connect the File's owner view to the UIView's view.




回答3:


I believe you are looking for something like this. You would replace "whatever" with the name of you second nib file.

newNavController = [[UINavigationController alloc] initWithNibName:@"whatever" bundle:[NSBundle mainBundle]];



回答4:


First, it looks like you have your UITabBarItems under the navigation controllers instead of directly under the UITabBarController. That may be part of your problem.

Second, when you add a UITabBarController in IB and and click on its icon in your list of top-level objects (your first screenshot), the attributes inspector will allow you to change the type of view controller for each of the tabs. Using this, you can change them all to navigation controllers, if you wish. Also, since you wanted to load custom views and view controllers from other nibs, if you look at the "View Controller" section at the bottom of the attributes inspector, you can select a nib from your project to load the view from. Assuming that nib's "File's Owner" is set to your UINavigationController subclass, it should all work fine.

All of this without a large amount of coding work, either. Let me know if you'd like screenshots for what I'm talking about in case you can't find these panels.




回答5:


I found the same warning.I have kept all view controller in separate xib files. I got rid off it by removing .nib name and keeping it empty.



来源:https://stackoverflow.com/questions/883510/loading-uinavigationcontroller-from-another-nib-automatically-by-uitabbarcontrol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!