Loading UINavigationController from another nib automatically by UITabBarController

后端 未结 5 769
無奈伤痛
無奈伤痛 2021-02-06 16:57

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 t

相关标签:
5条回答
  • 2021-02-06 17:10

    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.

    0 讨论(0)
  • 2021-02-06 17:11

    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.

    0 讨论(0)
  • 2021-02-06 17:24

    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.

    0 讨论(0)
  • 2021-02-06 17:29

    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.

    0 讨论(0)
  • 2021-02-06 17:32

    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]];
    
    0 讨论(0)
提交回复
热议问题