开发者

How to insert UINavigationController inside UITabBarController

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 cl开发者_高级运维ue why it may be. Maybe you have any clue? or point me to the place where such configuration is done.


I'll answer myself. It's good explained at https://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/CombiningViewControllers.html


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;
}


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


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/


- (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;

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜