IOS开发之路四(UITabBarController)
前两天看了看斯坦福大学的iphone开发公开课,讲的倒是不错,可看的我云里雾里的,不怎么讲基础和原理,不太适合初学者。今天看了一上午ios5基础教程这本书感觉有点头绪了。。。。废话少说,讲一讲我上午做的一个UITabBarController的例子。效果图如下:
过程:
1.新建一个empty IOS项目。
2,新建三个UIviewController
分别为:FirstViewController,SecondViewController,ThirdViewController
1.在 Xcode 中,选择文件菜单,然后选择 New—New File;
2.在 New File 对话框中,确保左侧的 iOS 类和子类中的 Cocoa Touch 已经选择。一旦完成以上操作,选择对话框右侧的 UIViewController 子类,并点击下一步,如图 2-25 所示:
图 2-25. 新视图控制器子类3.在下个页面,确认文档区域的子类显示 UIViewController,并确认没有选择 the
Targeted for iPad 的和 With IXB for User Interface 复选框,如图 2-26 所示。点击下一步。
iOS 5 Programming Cookbook www.devdiv.com 翻译整理
图 2-26. 一个无 xib 文件的客户视图控制器
4.在下页面(保存为),将你的视图控制器文件命名为 Root-ViewController 并点击保存
键,如图 2-27 所示。
图 2-27. 保存一个无 xib 文件的视图控制器
3.程序代理里的代码:
#import <UIKit/UIKit.h> @class FirstViewController; @class SecondViewController; @interface Presenting_Multiple_View_Controllers_with_UITabBarControllerAppDelegate : UIResponder <UIApplicationDelegate> @property (nonatomic, strong) UIWindow *window; @property (nonatomic, strong) FirstViewController *firstViewController; @property (nonatomic, strong) UINavigationController *firstNavigationController; @property (nonatomic, strong) SecondViewController *secondViewController; @property (nonatomic, strong) UINavigationController *secondNavigationController; @property (nonatomic, strong) UITabBarController *tabBarController; @end 既然我们已经声明到位了,那就开始在程序代理的编译文件里编译标签栏控件吧, 代码如下。 @synthesize window = _window; @synthesize firstViewController; @synthesize firstNavigationController; @synthesize secondViewController; @synthesize secondNavigationController; @synthesize tabBarController; - (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ // Override point for customization after application launch. self.window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]]; [self.window makeKeyAndVisible]; self.firstViewController = [[FirstViewController alloc] initWithNibName:nil bundle:NULL]; self.firstNavigationController = [[UINavigationController alloc] initWithRootViewController:self.firstViewController]; self.secondViewController = [[SecondViewController alloc] initWithNibName:nil bundle:NULL]; self.secondNavigationController = [[UINavigationController alloc] initWithRootViewController:self.secondViewController]; NSArray *twoNavControllers = [[NSArray alloc] initWithObjects: self.firstNavigationController, self.secondNavigationController, nil]; self.tabBarController = [[UITabBarController alloc] init]; [self.tabBarController setViewControllers:twoNavControllers]; [self.window addSubview:self.tabBarController.view]; return YES; }
FirstViewController里的代码如下:(另外两个相同)
// FirstViewController.m // TableBarViewController // // Created by WildCat on 13-8-5. // Copyright (c) 2013年 wildcat. All rights reserved. // #import "FirstViewController.h" @interface FirstViewController () @end @implementation FirstViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.title = @"First"; self.tabBarItem.image = [UIImage imageNamed:@"menu.png"]; } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self.view setBackgroundColor:[UIColor redColor]]; } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } @end