How to insert UINavigationController inside UITabB

2019-04-10 15:35发布

How to insert UINavigationController inside UITabBarController.

Currently I have main UITabBarController with declatarion inside application delegate like this (so tab is main)

self.window.rootViewController = self.tabBarController;

And inside one of tabs I want to insert UINavigationController, and can't get it.

The code is contructed like this:

  1. MainWindow.xib with UITabBarController object with tab typed as UINavigationController (point to NavigationHistory.xib) - screenshot: invalid link
  2. NavigationHistory.xib contains only UINavigationController where view point to History.xib
  3. History.xib have only UITableView element - screenshot: invalid link

And now UIViewController doesn't display my View1 view, and I have no clue why it may be. Maybe you have any clue? or point me to the place where such configuration is done.

5条回答
虎瘦雄心在
2楼-- · 2019-04-10 15:48

Add your controller object to UINavigationController, and add that navigationcontroller object to UITabBarController

查看更多
forever°为你锁心
3楼-- · 2019-04-10 15:50

UINavigationController is a subclass of UIViewController, a UITabBarController expects an array of UIViewControllers (and therefore UINavigationController ); this tells me me that I can give a UITabBarController an array of UINavigationControllers (or sublasses thereof) giving the desired interaction.

Ref : https://developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationController_Class/

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITabBarController_Class/

查看更多
对你真心纯属浪费
4楼-- · 2019-04-10 15:55
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UITabBarController *tab = [[UITabBarController alloc] init];


SimpleTableViewController *tableView = [[SimpleTableViewController alloc] init];
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:tableView];
UITabBarItem *item = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemHistory tag:0];
nav1.tabBarItem = item;


AboutViewController *about = [[AboutViewController alloc] init];
UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:about];
UITabBarItem *item2 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFeatured tag:0];
nav2.tabBarItem = item2;

tab.viewControllers = @[nav1,nav2];

self.window.rootViewController = tab;
[self.window makeKeyAndVisible];
return YES;

}

查看更多
▲ chillily
6楼-- · 2019-04-10 16:04

Write a code in appdelegate.m file.....

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    NSMutableArray *Mutablearray = [[NSMutableArray alloc] init];

        UIViewController *1st_View = [[FirstViewController alloc] initWithNibName:@"FirstViewController_iPhone" bundle:nil];

        UINavigationController *Navigation = [[UINavigationController alloc] initWithRootViewController:1st_View];

        [Mutablarray addObject:Navigation];

        UIViewController *2nd_View = [[SecondViewController alloc] initWithNibName:@"SecondViewController_iPhone" bundle:nil];

        UINavigationController *Navigation  = [[UINavigationController alloc] initWithRootViewController:2nd_View];
        [Mutablearray addObject:Navigation];

    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = Mutablearray;
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}
查看更多
登录 后发表回答