下面记一下怎样通过代码的方式为选项卡添加视图。

1、创建一个基于Empty Application的项目

2、创建两个新类,基类选择UIViewController,勾选With XIB for user interface分别命名为"OneController'和"TwoController",

3、分别更改OneController.xib和TwoController.xib文件的view背景颜色,便于区分

4、在AppDelegate.m文件中的 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ }函数做如下修改(记得导入OneController和TwoController的头文件)

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  2. {
  3. self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
  4. //将tabBar(选项卡)添加进来
  5. UITabBarController *tabBarController = [[[UITabBarController alloc] init] autorelease];
  6. //为选项卡添加子控制器
  7. OneController *one = [[[OneController alloc] init] autorelease];
  8. [tabBarController addChildViewController:one];
  9. TwoController *two = [[[TwoController alloc] init] autorelease];
  10. [tabBarController addChildViewController:two];
  11. self.window.rootViewController = tabBarController;
  12. [self.window makeKeyAndVisible];
  13. return YES;
  14. }
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; //将tabBar(选项卡)添加进来
UITabBarController *tabBarController = [[[UITabBarController alloc] init] autorelease]; //为选项卡添加子控制器
OneController *one = [[[OneController alloc] init] autorelease];
[tabBarController addChildViewController:one]; TwoController *two = [[[TwoController alloc] init] autorelease];
[tabBarController addChildViewController:two]; self.window.rootViewController = tabBarController; [self.window makeKeyAndVisible];
return YES;
}

运行效果如下:

现在创建好的选项卡下面是没有图标和文字的,,,现在我们通过代码给它们添加一些图标和文字,注意,,选项卡的图标和文字是子控制器决定的而不是tab Bar Controller,,这点要记住。

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  2. {
  3. self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
  4. //将tabBar(选项卡)添加进来
  5. UITabBarController *tabBarController = [[[UITabBarController alloc] init] autorelease];
  6. //为选项卡添加子控制器
  7. OneController *one = [[[OneController alloc] init] autorelease];
  8. one.tabBarItem = [[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:0] autorelease];//增加系统自带的下载图标
  9. [tabBarController addChildViewController:one];
  10. TwoController *two = [[[TwoController alloc] init] autorelease];
  11. //添加一个自定义的图标和文字
  12. two.tabBarItem.title = @"two";
  13. two.tabBarItem.image = [UIImage imageNamed:@"success.png"];
  14. [tabBarController addChildViewController:two];
  15. self.window.rootViewController = tabBarController;
  16. [self.window makeKeyAndVisible];
  17. return YES;
  18. }
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; //将tabBar(选项卡)添加进来
UITabBarController *tabBarController = [[[UITabBarController alloc] init] autorelease]; //为选项卡添加子控制器
OneController *one = [[[OneController alloc] init] autorelease];
one.tabBarItem = [[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:0] autorelease];//增加系统自带的下载图标
[tabBarController addChildViewController:one]; TwoController *two = [[[TwoController alloc] init] autorelease];
//添加一个自定义的图标和文字
two.tabBarItem.title = @"two";
two.tabBarItem.image = [UIImage imageNamed:@"success.png"];
[tabBarController addChildViewController:two]; self.window.rootViewController = tabBarController; [self.window makeKeyAndVisible];
return YES;
}

以上代码中,我在第一个Controll View 中添加了一个系统自带的下载图标,,在第二个Controller View中添加了一个自定义的图标(先将图标导入到项目中)和文字。

运行效果如下:

以上的所有代码我都是在AppDelegate.m文件中得

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ }函数中实现的,,,这仅仅只是为了操作方便才这样写的,,大多数情况下是写在该tab Bar Controller 的实现文件中的,如这里是在的MyTabController.m文件中的- (id)init{ } 函数中实现的。

tab bar controller的更多相关文章

  1. iOS第八课——Navigation Controller和Tab bar Controller

    今天我们要学习Navigation Controller和Tab bar Controller. Navigation Controller是iOS编程中比较常用的一种容器,用来管理多个视图控制器. ...

  2. iOS开发中的错误整理,Changing the delegate of a tab bar managed by a tab bar controller is not allowed

    iOS [错误:'Changing the delegate of a tab bar managed by a tab bar controller is not allowed.'] 错误:'Ch ...

  3. Tab Bar Controller和Navigation Controller混合使用详细教程

    在IPHONE上,NAV和TAB混合使用的案例很多.但很多书籍都没详细介绍这个是怎么使用的.我也找了很久才弄清楚怎么做.现在分享给大家. 1.先建立一个Window-based Application ...

  4. Iphone [Tab Bar实现多view切换,Picker,DataPicter实现

    用Tab Bar Controller处理IPhone多个view切换, 而且还附有创建空项目,picker和DataPicker的实现! 具体步骤: 1.创建一个空项目,选择User Interfa ...

  5. iOS开发:使用Tab Bar切换视图

    iOS开发:使用Tab Bar切换视图 上一篇文章提到了多视图程序中各个视图之间的切换,用的Tool Bar,说白了还是根据触发事件使用代码改变Root View Controller中的Conten ...

  6. Swift 4.0.2 按下tab bar item时, item会有内缩的animation效果(如同Twitter的tab bar 效果一样)

    先上效果图: 假设 tab bar items 有5个.tag为0,1,2,3,4.storyboard中tab bar controller继承的class叫做xxxVC. class xxxVC: ...

  7. 学习笔记:Tab Bar 控件使用详解

    注意这里是:Tab Bar 不是Tab Bar Controller. Tab bar是继承UIView,所以可以添加到ViewController里.是View就可以add到另一个View上去.Ta ...

  8. IOS学习之基于IOS7的tab bar

    转载请注明出处 http://blog.csdn.net/pony_maggie/article/details/28129473 作者:小马 什么是tabbar? 先几张图:      上图中蓝色框 ...

  9. uitabbarcontroller中 在设置tab bar item的image属性后不显示问题

    开始使用ios中的UITabBarController,在给Tab Bar Item设置自定义图片的时候,遇到了问题 按照如下配置: 出来的结果确是: 实际上test24.png应该是: 纠结了很久, ...

随机推荐

  1. python学习笔记2-条件语句

    #条件语句 ''' if 判断条件: 执行语句…… else: 执行语句…… ''' flag = False name = 'python' if name == 'python': # 判断变量否 ...

  2. ASP.NET Core MVC 2.x 全面教程_ASP.NET Core MVC 05.Controller 的路由

    视频地址: https://www.bilibili.com/video/av38392956/?p=5 这里面就包含了MVC相关的库 可以通过打开右侧的Nuget库进行查看 这里修改下 ,只需要静态 ...

  3. 斯坦福CS231n—深度学习与计算机视觉----学习笔记 课时5

    课时5 数据驱动的图像分类方式:K最邻近与线性分类器(下) 在参数化方法中,我们要做的是构造一个函数,输入一张图片,对每一类输出一个数值.对任意一张图片,我们会计算出它到底属于哪一类.这些参数有时候也 ...

  4. input required字段;django input输入框不填写会自动变红如何修改

    前端页面中,input不输入任何内容时,点击submit时,未填写的input会标红框,有些人还会有"该字段必填的字样"!! 什么鬼,你妹的,js也见不到,css3动画也见不到,怎 ...

  5. TensorFlow图像处理函数

    参考书 <TensorFlow:实战Google深度学习框架>(第2版) 图像编码处理+图像大小调整+图像翻转+图像色彩调整+处理标注框 #!/usr/bin/env python # - ...

  6. 洛谷 P1589 泥泞路

    题目描述 暴雨过后,FJ的农场到镇上的公路上有一些泥泞路,他有若干块长度为L的木板可以铺在这些泥泞路上,问他至少需要多少块木板,才能把所有的泥泞路覆盖住. 输入输出格式 输入格式: 第一行为正整数n( ...

  7. 跟我一起玩Win32开发(12):使用控件——单选按钮

    今天,咱们还是接着玩“控件斗地主”,这是我原创的超级游戏,有益身心健康,玩一朝,十年少. 哦,对,脑细胞极速运动了一下,想起了一个问题,这个破问题虽然网上有很多种解决方案,但是,并没有让所有人都解决问 ...

  8. Codeforces Round #418 (Div. 2) A

    Description A few years ago, Hitagi encountered a giant crab, who stole the whole of her body weight ...

  9. Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) D

    Description A tree is an undirected connected graph without cycles. The distance between two vertice ...

  10. ACM_扫雷(dfs)

    扫雷 Time Limit: 2000/1000ms (Java/Others) Problem Description: 扫雷这个游戏想必各位都是会玩的吧.简单说一下规则,n行m列的格子地图上有分布 ...