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

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. 斯坦福CS231n—深度学习与计算机视觉----学习笔记 课时4

    课时4 数据驱动的图像分类:K最邻与线性分类器(上) 图像分类之前,我们需要将图片转换成一张巨大的数字表单,然后从所有种类中,给这个表单选定一个标签. 为什么分类问题是个困难的问题:图像分类难点是,当 ...

  2. 洛谷 - P2598 - 狼和羊的故事 - 最大流

    https://www.luogu.org/problemnew/show/P2598 第一次写这种修篱笆的题目,上次好像晓阳dalao写了一个堵人的.好像还有能改变土地属性的. #include&l ...

  3. 你真的懂了redis的数据结构吗?redis内部数据结构和外部数据结构揭秘

    原文链接:https://mp.weixin.qq.com/s/hKpAxPE-9HJgV6GEdV4WoA Redis有哪些数据结构? 字符串String.字典Hash.列表List.集合Set.有 ...

  4. jdbc 连接 sqlserver 学习

    使用sqljdbc.jar 连接sqlserver 下载网址: http://www.drv5.cn/sfinfo/8228.html#softdown package test_sql_server ...

  5. 黑客攻防技术宝典web实战篇:攻击本地编译型应用程序习题

    猫宁!!! 参考链接:http://www.ituring.com.cn/book/885 随书答案. 1. 如果不采用特殊的防御措施,为什么栈缓冲区溢出比堆溢出更容易被攻击者利用? 利用基于栈的溢出 ...

  6. 使用c++的一些建议

    1: 不要使用宏,用const或enum定义常量 用inline避免函数的额外调用(使用inline的函数,块里面尽量不要使用循环和递归) 用template去荷花一些函数或者类型 用namespac ...

  7. [NewTrain 10][poj 2329]Nearest Number - 2

    题面: http://poj.org/problem?id=2329 题解: 这题有很多做法 1. 搜索 复杂度$O(n^4)$ 但是实际上远远达不到这个复杂度 所以可以通过 2. 对于每一个格子,我 ...

  8. Unity3d中UnityEngine.Object

    UnityEngine.Object继承自system.Object,是Unity所涉及所有物体的基类. Static Functions 静态函数   下面的都是静态函数 Destroy Remov ...

  9. 动手实现 React-redux(五):Provider

    我们要把 context 相关的代码从所有业务组件中清除出去,现在的代码里面还有一个地方是被污染的.那就是 src/index.js 里面的 Index: ... class Index extend ...

  10. 自動獲取外網IP并發郵件

    問題: 公司有一Web系統需開放給香港Office公司查詢資料,但最近動態域名需實名認證, 因系統較小型,非公開大範圍使用,所以再認證一域名也沒多大必要, 所以想定時生成一封能查詢外網IP的郵件發送給 ...