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

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. 任务47:Identity MVC:ReturnUrl实现

    任务47:Identity MVC:ReturnUrl实现 在最上面加一个私有的方法 登陆也加上returnUrl Login的post方法.加入returnUrl的参数 登陆界面也需要加上 asp- ...

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

    课时7 线性分类器损失函数与最优化(下) 我们为什么要最大化对数概率而非直接最大化概率? 你在做逻辑斯蒂回归时,如果你只是想要最大化概率,那你使用log是无意义的.因为log函数是单调函数,最大化概率 ...

  3. bzoj 4259 4259: 残缺的字符串【FFT】

    和bzoj 4503 https://www.cnblogs.com/lokiii/p/10032311.html 差不多,就是再乘上一个原串字符 有点卡常,先在点值下算最后一起IDFT #inclu ...

  4. 命令行运行Java程序时出现错误

    在命令行运行Java程序时出现下面错误 Error: Could not create the Java Virtual Machine. Error: A fatal exception has o ...

  5. two_sum问题

    def two_sum(li, target): for i in range(len(li)): for j in range(i+1, len(li)): if li[i] + li[j] == ...

  6. [WOJ1318]和最大

    题目链接: WOJ1318 题目分析: 首先我们要知道当这是一个线性的序列的时候应该怎么做:最大子序和 这里是线性的,就把数组复制两遍即可 好像有些细节要处理(也可能是我代码写丑了),具体的都在代码里 ...

  7. [APIO2012]派遣 洛谷P1552 bzoj2809 codevs1763

    http://www.codevs.cn/problem/1763/ https://www.lydsy.com/JudgeOnline/problem.php?id=2809 https://www ...

  8. VS Code 自用插件备份

    自用插件备份 Auto Close Tag 自动闭合标签 Atuo Rename Tag 更改前面标签的时候, 自动更改后面的闭合标签 Guides 对齐线 open-in-browser 在浏览器中 ...

  9. 转 OGG Troubleshooting-Database error 1 (ORA-00001: unique constraint ...)

    Q5: After imp data to target, when we start replc process, we find the following error: 2011-11-10 0 ...

  10. 一个因xdata声明引起的隐含错误

    我们知道一般增强型c51自身的RAM只有128BYTES,根本不够用,所以一般在定义全局变量,静态变量时都要用XDATA作为关键字修饰数据的的存储类型.但要注意的是,定义和声明一定要一致,不然出现错误 ...