IOS学习之Tab Bar的使用和视图切换
<div id="cnblogs_post_body">在ios的程序中,Tab Bar的使用率很高,几个视图需要切换的时候,就用到tabbar。今天的程序实现的效果是这样的,底部有几个tab Item,对应的有几个视图,切换tab Item,切换到对应的视图。
实现效果如下:
http://pic002.cnblogs.com/images/2012/322584/2012092522121339.pnghttp://pic002.cnblogs.com/images/2012/322584/2012092522125746.png
为了更好理解使用用tabbar和切换视图,我们创建一个Empty Application。
1、打开Xcode ,新建项目
http://pic002.cnblogs.com/images/2012/322584/2012092522140149.png
2、创建View Controller
在项目上按花键+N创建新文件,创建 Objective-C class 文件,按Next按钮,subClass 选UIViewController 。勾选上xib选项
http://pic002.cnblogs.com/images/2012/322584/2012092522144350.png
以同样方式创建另外三个ViewController ,RedViewController ,GreyViewController,YellowViewController。四个View准备好了。那么Tabbar呢?
3、创建TabBarController.xib文件,选择创建Empty文件
这时候你发现创建的xib文件是空白的,不用慌,去右下角控件栏中把TabBar Controller拖过来就Ok了。
http://my.csdn.net/uploads/201205/30/1338353421_3238.png
4、关联TabBarController.xib ,tabbarAppDelegate这两个文件
在上图中选择File&rsquo;s Owner,打开Identity Inspector,在Class一栏选择tabbarAppDelegate
http://my.csdn.net/uploads/201205/30/1338353445_7226.png
这样,我们就可以创建TabBarController.xib 文件指向tabbarAppDelegate 文件的Outlet映射了。
5、在Xcode中的工具栏的View菜单找到 打开Assistant Editor,使tabbarAppDelegate.h和TabBarController.xib 同时打开。
在xib文件上按住control键,往tabbarAppDelegate.h,创建Outlet.
http://pic002.cnblogs.com/images/2012/322584/2012092522161876.png
弹出窗口输入 rootController,点connect。
6、添加代码
打开tabbarAppDelegate.m,在didFinishLaunchingWithOptions方法中添加代码:
<div class="cnblogs_code">- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ self.window = [ initWithFrame:[ bounds]]; // Override point for customization after application launch. [ loadNibNamed:@"TabBarController" owner:self options:nil]; ; self.window.backgroundColor = ; ; return YES;}
页:
[1]