IOS系列——Tabbarcontroller的含义

来源:互联网 发布:弱电设计软件 编辑:程序博客网 时间:2024/06/03 20:56

之前写了一篇简单的文章,是开始使用Tabbar的,这一篇文章还是讲一些简单的东西,不过涉及到一些概念

在使用tabbar的时候,我们必须了解UITabBarController结构与其各个相关类的关系(TabBarController、TabBar、TabBarItem及ViewController)

1.TabBarItem:是一个双态的Button(选中和未选中),行为和CheckBox、RadioButton类似。

2.TabBar:是TabBarItem的容器,负责TabBarItem的排布和互斥,保证同时只有一个Item为选中态。(TabBar就是下图中黑色的那一条,Item是黑色当中的一个单元)

3.TabBarController: 包含了TabBar,并管理这一个ViewController的栈,在TabBar上的按钮点击时对栈上的ViewController位置进行相应的调整,从而保持TabBar和ViewController栈之间的一致性。

4.ViewController :这个和TabBarController没有任何的直接关系,他是一个单独的视图(view),只不过在和TabBarController结合使用的时候,是通过TabBar中得TabBarItem来管理这个ViewController。

首先借助官方文档的一张图来看

最右边的那张图,就是整个页面(看标注的厚度)就是我们通常所说的Tabbarcontroller

最左边的那张图,就是我们所说的tabbar(在这里包含了四个TabBarItem)

中间的那张图,就是自定义内容,也就是我们需要通过TabBarItem老关联并且控制的ViewController。

我们可以通过下面的这已小段代码来了解

    UITabBar *tab = [[UITabBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];    [self.view addSubview:tab];        UITabBarItem * tabBarItem1 = [[UITabBarItem alloc] initWithTitle:@"1" image:[UIImage imageNamed:@"ic_reset.png"] tag:0];    UITabBarItem * tabBarItem2 = [[UITabBarItem alloc] initWithTitle:@"2" image:nil tag:1];    UITabBarItem * tabBarItem3 = [[UITabBarItem alloc] initWithTitle:@"3" image:nil tag:2];    UITabBarItem * tabBarItem4 = [[UITabBarItem alloc] initWithTitle:@"4" image:nil tag:3];    NSArray *tabBarItemArray = [[NSArray alloc] initWithObjects: tabBarItem1, tabBarItem2, tabBarItem3, tabBarItem4,nil];    [tab setItems: tabBarItemArray];        [tabBarItem1 release];    [tabBarItem2 release];    [tabBarItem3 release];    [tabBarItem4 release];    [tabBarItemArray release];    [tab release];
就得到了这样的一个效果


原创粉丝点击