IOS开发之路四(UITabBarController)

前两天看了看斯坦福大学的iphone开发公开课,讲的倒是不错,可看的我云里雾里的,不怎么讲基础和原理,不太适合初学者。今天看了一上午ios5基础教程这本书感觉有点头绪了。。。。废话少说,讲一讲我上午做的一个UITabBarController的例子。效果图如下:

过程:

1.新建一个empty IOS项目。


2,新建三个UIviewController

分别为:FirstViewController,SecondViewController,ThirdViewController

1.在 Xcode 中,选择文件菜单,然后选择 NewNew 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.程序代理里的代码:

  1. #import <UIKit/UIKit.h>  
  2. @class FirstViewController;  
  3. @class SecondViewController;  
  4. @interface Presenting_Multiple_View_Controllers_with_UITabBarControllerAppDelegate : UIResponder <UIApplicationDelegate>  
  5. @property (nonatomic, strong) UIWindow *window;  
  6. @property (nonatomic, strong) FirstViewController *firstViewController;  
  7. @property (nonatomic, strong)  
  8. UINavigationController *firstNavigationController;  
  9. @property (nonatomic, strong) SecondViewController *secondViewController;  
  10. @property (nonatomic, strong)  
  11. UINavigationController *secondNavigationController;  
  12. @property (nonatomic, strong) UITabBarController *tabBarController;  
  13. @end  
  14. 既然我们已经声明到位了,那就开始在程序代理的编译文件里编译标签栏控件吧,  
  15.   
  16. 代码如下。  
  17. @synthesize window = _window; @synthesize firstViewController; @synthesize firstNavigationController; @synthesize secondViewController; @synthesize secondNavigationController; @synthesize tabBarController;  
  18. - (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ // Override point for customization after application launch. self.window = [[UIWindow alloc] initWithFrame:  
  19. [[UIScreen mainScreen] bounds]];  
  20. [self.window makeKeyAndVisible];  
  21. self.firstViewController = [[FirstViewController alloc] initWithNibName:nil  
  22. bundle:NULL];  
  23. self.firstNavigationController =  
  24. [[UINavigationController alloc] initWithRootViewController:self.firstViewController]; self.secondViewController = [[SecondViewController alloc] initWithNibName:nil  
  25. bundle:NULL];  
  26. self.secondNavigationController =  
  27. [[UINavigationController alloc] initWithRootViewController:self.secondViewController]; NSArray *twoNavControllers = [[NSArray alloc] initWithObjects:  
  28. self.firstNavigationController, self.secondNavigationController, nil];  
  29. self.tabBarController = [[UITabBarController alloc] init]; [self.tabBarController setViewControllers:twoNavControllers]; [self.window addSubview:self.tabBarController.view];  
  30. return YES;  
  31. }  


FirstViewController里的代码如下:(另外两个相同)

    1. //  FirstViewController.m  
    2. //  TableBarViewController  
    3. //  
    4. //  Created by WildCat on 13-8-5.  
    5. //  Copyright (c) 2013年 wildcat. All rights reserved.  
    6. //  
    7.   
    8. #import "FirstViewController.h"  
    9.   
    10. @interface FirstViewController ()  
    11.   
    12. @end  
    13.   
    14. @implementation FirstViewController  
    15.   
    16. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
    17. {  
    18.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
    19.     if (self) {  
    20.         self.title = @"First";  
    21.         self.tabBarItem.image = [UIImage imageNamed:@"menu.png"];  
    22.     }  
    23.     return self;  
    24. }  
    25.   
    26. - (void)viewDidLoad  
    27. {  
    28.     [super viewDidLoad];  
    29.     // Do any additional setup after loading the view.  
    30.     [self.view setBackgroundColor:[UIColor redColor]];  
    31. }  
    32.   
    33. - (void)viewDidUnload  
    34. {  
    35.     [super viewDidUnload];  
    36.     // Release any retained subviews of the main view.  
    37. }  
    38.   
    39. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
    40. {  
    41.     return (interfaceOrientation == UIInterfaceOrientationPortrait);  
    42. }  
    43.   
    44. @end  
原文地址:https://www.cnblogs.com/lixingle/p/3274751.html