前两天看了看斯坦福大学的iphone开发公开课,讲的倒是不错,可看的我云里雾里的,不怎么讲基础和原理,不太适合初学者。今天看了一上午ios5基础教程这本书感觉有点头绪了。。。。废话少说,讲一讲我上午做的一个UITabBarController的例子。效果图如下:
1.新建一个empty IOS项目。
2,新建三个UIviewController
分别为:FirstViewController,SecondViewController,ThirdViewController
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.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