前面有一篇博文iOS学习之Tab Bar的使用和视图切换 这是在AppDelegate里使用Tabbar,这样的程序打开就是TabbarView了,有时候我们需要给程序做一些帮助页面,或者登录页面,之后才跳转到tabbar
View里,或者后面的页面才使用Tabbar的,那这样怎么实现呢?

我们建立一个视图,然后在这个视图通过[selfpresentModalViewController : tabBaranimated:YES];跳转来实现。

当程序中需要在多个View直接切换的时候,可以使用 UINavigationController,也可以用 ModalViewController。UINabigationController 是通过导航条来切换多个 view。而如果 view 的数量比较少,且显示领域为全屏的时候,用 ModalViewController 就比较合适(比如需要用户输入信息的view,结束后自动回复到之前的view)

1、新建一个Single View app,按Command + N新建三个ViewController ,都选上.xib文件。

1.1 新建的Controller分别是:TestOneController TestTwoController  TestThirdViewController ,他们都继承UIViewController。

单击xib文件,在.xib文件的属性窗口里修改View的颜色,这样好在切换页面的时候区分出来是切换了页面。

好吧,我的ThirdViewController没有xib,可能是漏了,不过也没关系,一样能用。

1.2 添加TabBarViewController

最重要的是再添加一个TabBarViewController,这个需要继承的UITabBarController

2、在ViewController也就是程序进来的第一个页面。在这里添加一个跳转的Button,并加上Action,然后在Action里实现跳转到tabbar.

ViewController.m里实现代码。这就跳转,把刚才建立的三个ViewController都添加到Tabbar里

  1. - (IBAction)gotoTabbarVIew:(id)sender {
  2. NSMutableArray *items = [[NSMutableArray alloc] init];
  3. TestOneController *testOne1 = [[TestOneController alloc] init];
  4. [items addObject:testOne1];
  5. TestTwoController *twoController = [[TestTwoController alloc] init];
  6. [items addObject:twoController];
  7. TestThirdViewController *thirdController = [[TestThirdViewController alloc] init];
  8. [items addObject:thirdController];
  9. // items是数组,每个成员都是UIViewController
  10. TabBarViewController *tabBar = [[TabBarViewController alloc] init];
  11. [tabBar setTitle:@"TabBarController"];
  12. [tabBar setViewControllers:items];
  13. [self presentModalViewController : tabBar animated:YES];
  14. }

这样运行跳转到TabbarView里的了,但是现在的tabbarView里面的三个Tab 按钮都空白的黑色。怎么添加图标和其他样式呢?

3、添加UITabBarItem

在三个ViewController.m文件里添加对应的UITabBarItem,代码如下

  1. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  2. {
  3. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  4. if (self) {
  5. UITabBarItem *item = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemMostRecent tag:1];
  6. self.tabBarItem = item;
  7. self.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d",9];
  8. }
  9. return self;
  10. }

UITabBarSystemItemMostRecent这个item的风格常量,可以根据喜好改变。除此之外还可以用自定义的图片做为item。这里就不演示自己添加图片的方式了。

self.tabBarItem.badgeValue 这个在tabbar item上显示泡泡的数字。

对应的其他ViewController都添加上,tag写不同的数字,后面要用到的。现在运行就有效果了

切换

4、监听Item的点击事件

Tabbar有了,怎么监听你点了哪个item呢?

实现UITabBarDelegate。在apple的文档里查了一下,实现

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item这个方法即可监听

  1. - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
  2. {
  3. if(item.tag == 1){
  4. NSLog(@"TestOneController");
  5. }else if(item.tag == 2){
  6. NSLog(@"TestTwoController");
  7. }else {
  8. NSLog(@"TestThirdController");
  9. }
  10. }

通过tag判断,运行切换:打印log。

  1. 2012-06-28 20:56:19.144 View2TaBBarView[5614:f803] TestTwoController
  2. 2012-06-28 20:56:19.785 View2TaBBarView[5614:f803] TestThirdController
  3. 2012-06-28 20:56:20.363 View2TaBBarView[5614:f803] TestTwoController
  4. 2012-06-28 20:56:20.843 View2TaBBarView[5614:f803] TestOneController

5、返回上个页面

通过    [self.parentViewControllerdismissModalViewControllerAnimated:YES];
返回上个页面

在ThirdView添加一个button。添加Action事件。代码如下:

  1. -(void)backAction:(id)sender{
  2. [self.parentViewController dismissModalViewControllerAnimated:YES];
  3. }
  4. - (void)viewDidLoad
  5. {
  6. [super viewDidLoad];
  7. [self.view setBackgroundColor:[UIColor brownColor]];
  8. UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(40, 50, 60, 30)];
  9. [button setTitle:@"返回" forState:UIControlStateNormal];
  10. [button addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
  11. [self.view addSubview:button];
  12. // Do any additional setup after loading the view.
  13. }

运行,点返回button,返回了第一个页面了:

例子的代码:http://download.csdn.net/detail/totogo2010/4399715

著作权声明:本文由http://blog.csdn.net/totogo2010/原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢!

最新文章

  1. Python list
  2. Mybatis 批量insert
  3. csuoj 1113: Updating a Dictionary
  4. Call C# in powershell
  5. Bootstrap入门五:表格
  6. Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)
  7. IMX515开发备忘
  8. JAXB - Hello World with Namespace
  9. python刷取CSDN博文访问量之一
  10. HH的军训(容斥)
  11. C#数学运算表达式解释器
  12. bzoj 4825: [Hnoi2017]单旋 [lct]
  13. Jenkins实践之入门体验
  14. unity 常用插件 3
  15. MyEclipse Web项目部署失败:Deployment failure on Tomcat 7.x.Could not copy all resources to XXX.
  16. Matlab:五点差分方法求解椭圆方程非导数边值问题
  17. [PHP]算法- 判断是否为二叉搜索树的后序遍历序列的PHP实现
  18. e藏在哪里?
  19. spring mvc处理方法返回方式
  20. PHP 7 安装 Memcache 和 Memcached 总结

热门文章

  1. Python学习-str与byte类型以及编码
  2. 《DSP using MATLAB》示例Example 9.7
  3. C#连接数据库以及增、删、改、查操作
  4. Sublime Text 3常用插件安装
  5. CentOS 7.0 yum安装Apache、PHP和MySQL
  6. PAT 1006 换个格式输出 C语言
  7. 记录一些WPF常用样式方便以后复用(转)
  8. 笔记:Node.js 的 Buffer 缓冲区
  9. vs2017 xamarin导入jar,SO文件的问题
  10. 让C# Excel导入导出,支持不同版本的Office(转)