UINavigationController和UITabBarController的集成

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值