UINavigationController和UITabBarController的集成

来源:互联网 发布:java简单小程序代码 编辑:程序博客网 时间:2024/04/28 15:07

UINavigationController(导航控制器)和UITabBarController(分栏控制器)的集成有以下两种形式:

    第一种:在TabBar控制器中某一个Tab中使用Navigation控制器,这是最常见的一种形式,下面会详细解说;

    第二种:在一个Navigation控制器控制下的某一个或某一些控制器是TabBar控制器,这是在对该TabBar控制器进行压入和弹出时与对普通视图控制器的操作方法是一样的。这种方法是不推荐使用的,据说是会出问题的。它的官方文档里有这样一段话:

      Because the UITabBarController class inherits from the UIViewController class, tab bar controllers have their own view that is accessible through the view property. When deploying a tab bar interface, you must install this view as the root of your window. Unlike other view controllers, a tab bar interface should never be installed as a child of another view controller.

所以我们在这里只说一下第一种形式的集成。

也就是说将Navigation作为TabBar控制器里某一个TabBarItem所对应的视图控制器。代码如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];    self.window.backgroundColor = [UIColor whiteColor];
    //OneViewController、TwoViewController和ThreeViewController是自己定义的继承自UIViewController的视图控制器类    OneViewController *oneVC = [[OneViewController alloc]init];    UINavigationController *oneNav = [[UINavigationController alloc]initWithRootViewController:oneVC];    TwoViewController *twoVC = [[TwoViewController alloc]init];    ThreeViewController *threeVC = [[ThreeViewController alloc]init];    //注意这里controllerAry里的内容是oneNev,twoVC和threeVC。即第一项是一个UINavigationController类的对象,第二项和第三项是UIViewController类的对象。    NSArray *controllerAry = [[NSArray alloc]initWithObjects:oneNav,twoVC,threeVC, nil];        [oneNav release];    [oneVC release];    [twoVC release];    [threeVC release];    
    //tabBarConller是定义在AppDelegate.h里的UITabBarController类型的实例变量。    //@property(nonatomic,retain) UITabBarController *tabBarconller;    tabBarConller = [[UITabBarController alloc]init];    [tabBarConller.tabBar setBackgroundImage:[UIImage imageNamed:@"beijing.png"]];    tabBarConller.viewControllers = controllerAry;    tabBarConller.selectedIndex = 0;        //设置每一项tabBarItem的属性(title和image)    [(UITabBarItem *)[tabBarConller.tabBar.items objectAtIndex:0] setTitle:@"第一页"];    [(UITabBarItem *)[tabBarConller.tabBar.items objectAtIndex:0]setImage:[UIImage imageNamed:@"one.png"]];    [(UITabBarItem *)[tabBarConller.tabBar.items objectAtIndex:1] setTitle:@"第二页"];    [(UITabBarItem *)[tabBarConller.tabBar.items objectAtIndex:1]setImage:[UIImage imageNamed:@"two.png"]];    [(UITabBarItem *)[tabBarConller.tabBar.items objectAtIndex:2] setTitle:@"第三页"];    [(UITabBarItem *)[tabBarConller.tabBar.items objectAtIndex:2]setImage:[UIImage imageNamed:@"three.png"]];        [self.window addSubview:tabBarConller.view];    //将tabBarController设置为window的根视图    self.window.rootViewController = self.tabBarConller;        [self.window makeKeyAndVisible];    [controllerAry release];        return YES;}


以上的代码就创建好了一个以UINavigationController类的实例作为tabBatController里tabBar上的第一个tabBarItem所对应的视图控制器的实例。而OneViewController的实例oneVC是作为oneNav的rootViewController的。


0 0
原创粉丝点击