本节要点:(重点:1、导航控制器,2、标签控制器)
了解几种常用的视图控制器
理解视图控制器的作用
理解和掌握导航控制器及标签控制器的用法
1、常见的视图控制器
--UIViewController
UIViewController是视图控制器的父类,其他视图控制器类都继承它。每个视图控制器都有一个单独的主视图,这是它独有的视图,用view属性来表示。
--UINavigationController
UINavigationController允许在树状视图层次结构间上下导航。它维护一个视图控制器的栈,任何类型的视图控制器都可以放入栈中。
--UITabBarController
当应用程序需要许多不同的交互模式或者不同的方式来查看相同的数据时,我们就可以考虑使用标签栏。
--UITableViewController
UITableViewController --表格视图控制器简化了iphone项目中的表视图的使用。它提供了一个标准的已连接UITableView实例并自动将委托和数据源设为指向自己,只需要提供这些委托和数据源方法,用数据填充表格并相应用户单击即可。
---地址薄控制器
地址薄用户界面框架提供懒人几个视图控制器,允许从地址薄中选择某个人,查看他的详细信息并添加新联系人或修改现有联系人条目。
--UIImagePickerController
图像选取器界面是通过名为UIImagePickerConttroller的模式控制器类执行的
。
-- 邮件撰写
MFMailComposeViewController允许创建用户可以直接在程序中定制邮件消息。
--GKPeerPickerController
尽管该控制器是GameKit的一部分,但其技术也很适合用于非游戏用途,不如文件传输、消息传递等。
--Media Player 控制器
Media Player 框架提供了几个控制器,允许选择和播放音乐、电影。
视图控制器的作用
-- 视图控制器的主要职责
管理其视图的显示,对事件做出响应,例如,iPhone显示方向或低内存情况等事件。
充当用户界面和应用程序数据模型之间的协调者(后面我们将详细介绍IOS中的MVC模式)。
-- 介绍几个使用视图控制器时必须了解的函数:
(1)loadView (2)viewDidLoad (3)viewDidUnload (4)viewWillAppear
(5)viewDidAppear (6)viewWillDisappear (7)viewDidDisappear
视图控制器的主要职责
--- 响应方向变化
当iPhone 旋转的时候,立即调用shouldAutorotateToInterfaceOrientation:方法
--处理低内存情况
当一个视图没有父视图或者当发布一条内存警告的时候,每个视图控制器通过didReceiveMemoryWarning方法接受到一条通知。
使用导航控制器添加事件,响应一个自定义方法,代码:
//在.h中声明一个方法
-(IBAction)pressToNextPage:(id)sender;
//在.m文件中实现这个方法
-(IBAction)pressToNextPage:(id)sender
{
//创建SecondViewController实例对象
SecondViewController* secondVC =
[[[SecondViewController alloc]init] autorelease];
//设置该视图的标题
secondVC.title = @"第二页";
//将实例对象secondVC压入导航控制栈中
[self,navigationController pushViewController:secondVC animated:YES];
}
使用导航控制器
-- 压入导航栈中代码如下:
[self.navigationController pushViewController:视图实例对象animated:YES];
使用标签控制器
--我们可以使用UITabBarController类来创建多页的界面。通过从位于屏幕底部的一个标签页栏选择一个标签页来显示页面。
--标签栏控制器的每个页面都是一个视图控制器。
在BoAppDelegate.h文件中:
#import <UIKit/UIKit.h>
@class BoRootViewController;
@interface BoAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (retain, nonatomic) BoRootViewController *mRootVC;
@end
在BoAppDelegate.m文件中
#import "BoAppDelegate.h"
#import "BoRootViewController.h"
@implementation BoAppDelegate
- (void)dealloc
{
[_mRootVC release];
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.window.backgroundColor = [UIColor whiteColor];
BoRootViewController *pRootVC = [[BoRootViewController alloc]initWithNibName:nil bundle:nil];
self.mRootVC = pRootVC;
[pRootVC release];
UINavigationController *pNav = [[UINavigationController alloc]initWithRootViewController:self.mRootVC];
self.window.rootViewController = pNav;
[pNav release];
[self.window makeKeyAndVisible];
//设置应用程序的小徽章
[UIApplication sharedApplication].applicationIconBadgeNumber = 2;
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
}
- (void)applicationWillTerminate:(UIApplication *)application
{
}
@end
在BoRootViewController.h文件中
#import <UIKit/UIKit.h>
@interface BoRootViewController : UIViewController
@end
在BoRootViewController.m文件中
#import "BoRootViewController.h"
#import "BoNextViewController.h"
@interface BoRootViewController ()
@end
@implementation BoRootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.navigationItem.title = @"根视图";
[self creatBarItem:nil];
//设置返回按钮的隐藏
//[self.navigationController setHidesBottomBarWhenPushed:YES];
[self creatTitleView:nil];
}
return self;
}
//创建自定义titleView
- (void)creatTitleView:(id)sender
{
UIView *pTitleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 160, 44)];
[pTitleView setBackgroundColor:[UIColor redColor]];
self.navigationItem.titleView = pTitleView;
}
//创建自定义按钮(barButtonItem)
- (void)creatBarItem:(id)sender
{
UIBarButtonItem *pBar = [[UIBarButtonItem alloc]initWithTitle:@"NextVC" style:UIBarButtonItemStylePlain target:self action:@selector(goToNextVC:)];
self.navigationItem.rightBarButtonItem = pBar;
}
- (void)goToNextVC:(id)sender
{
BoNextViewController *pNextVC = [[BoNextViewController alloc]initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:pNextVC animated:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end
在BoNextViewController.h文件中
#import <UIKit/UIKit.h>
@interface BoNextViewController : UIViewController
@end
在BoNextViewController.m文件中
#import "BoNextViewController.h"
@interface BoNextViewController ()
@end
@implementation BoNextViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.navigationItem.title = @"第二级视图";
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self creatBtn:nil];
}
- (void)creatBtn:(id)sender
{
UIButton *pBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[pBtn setFrame:CGRectMake(100, 100, 100, 40)];
[pBtn setTitle:@"TouchMe" forState:UIControlStateNormal];
[pBtn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:pBtn];
}
#pragma mark-----SEL:btnPressed-----
- (void)btnPressed:(id)sender
{
self.navigationController.toolbarHidden = !self.navigationController.toolbarHidden;
[sender setTitle:self.navigationController.toolbarHidden?@"Show":@"hidden" forState:UIControlStateNormal];
self.navigationItem.prompt = @"加载中";
[self performSelector:@selector(setPromptNil:) withObject:nil afterDelay:2];
}
- (void)setPromptNil:(id)sender
{
self.navigationItem.prompt = nil;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end