参照IPhone4 基础开发教程第六章:多视图应用程序
多视图程序可以在一个应用程序中切换多个视图
简单的多视图 程序从Window-based Application开始构建。
该程序需要在两个视图YellowView和BlueView之间切换。
程序切换需要一个控制器SwitchViewController来控制视图之间的切换, 视图中使用Toolbar控件
主控制器SwitchViewController文件
//
// SwitchViewController.h
// test08
//
// Created by lily on 12-2-1.
// Copyright 2012年 lily. All rights reserved.
//
#import<UIKit/UIKit.h>
//引入将要使用的类变量的类型
@class YellowViewController;
@class BlueViewController;
@interface SwitchViewController :UIViewController {
//定义2个实例变量指向YellowView和BlueView的控制器
YellowViewController *yellowViewController;
BlueViewController *blueViewController;
}
@property (nonatomic,retain)YellowViewController *yellowViewController;
@property (nonatomic,retain)BlueViewController *blueViewController;
- (IBAction) switchViews:(id)sender;
@end
//
// SwitchViewController.m
// test08
//
// Created by lily on 12-2-1.
// Copyright 2012年 lily. All rights reserved.
//
#import"SwitchViewController.h"
//如果在.h文件中引入了@class, 那么在这里需要引入class文件的头文件
#import"YellowViewController.h"
#import"BlueViewController.h"
@implementation SwitchViewController
@synthesize yellowViewController;
@synthesize blueViewController;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
returnself;
}
- (void)dealloc
{
[yellowViewController release];
[blueViewController release];
[superdealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[superdidReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
//如果控制器控制的视图变量没有父视图,即没被显示。
//那么在收到内存警报时,设置该视图为nil,以释放内存。
if(self.blueViewControlle.view.superview == nil)
self.blueViewController =nil;
if(self.yellowViewController.view.superview == nil)
self.yellowViewController = nil;
}
#pragma mark - View lifecycle
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
//在初始化视图时,初始化并加载BlueView
//初始化BlueView
//通过initWithNibName方法,blueController初始化时加载的.xib界面文件
//注意:指明.xib文件时,键入全名,但不需要加入.xib后缀
BlueViewController *blueController = [[BlueViewController alloc]initWithNibName:@"BlueView" bundle:nil];
self.blueViewController = blueController;
//加载BlueView, atIndex:0表示加载在其他所有视图之后
[self.viewinsertSubview:blueController.view atIndex:0];
[blueController release];
[superviewDidLoad];
}
//控制视图的切换
- (IBAction)switchViews:(id)sender
{
//设置动画,这块只是不太看得明白,先放着吧
//启动动画
[UIView beginAnimations:@"View Flip" context:nil];
//动画时长
[UIView setAnimationDuration:1.25];
//动画曲线,UIViewAnimationCurveEaseInOut表示动画开始和结束时较快
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//如果YellowView没有父视图
if(self.yellowViewController.view.superview == nil)
{
//如果YellowView还未被初始化,或者内存被收回,那么初始化它
if(self.yellowViewController ==nil)
{
YellowViewController *yellowController = [[YellowViewController alloc]initWithNibName:@"YellowView" bundle:nil];
self.yellowViewController = yellowController;
[yellowController release];
}
//设置视图转换类型, UIViewAnimationTransitionFlipFromRightforView 表示从右向左翻页
//运行程序的时候,发现并没有扛到视图切换的动画效果,关于该设置表示不太理解,到现在也明白
[UIViewsetAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
//这段动画设置同样不明白
[blueViewController viewWillAppear:YES];
[yellowViewController viewWillDisappear:YES];
//去除视图中的BlueView
[blueViewController.view removeFromSuperview];
//向视图加载YellowView
[self.viewinsertSubview:yellowViewController.view atIndex:0];
//这段动画设置同样不明白
[yellowViewController viewDidDisappear:YES];
[blueViewController viewDidAppear:YES];
}
else
{
if(self.blueViewController ==nil){
BlueViewController *blueController = [[BlueViewController alloc]initWithNibName:@"BlueView" bundle:nil];
self.blueViewController = blueController;
[blueController release];
}
[UIViewsetAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.viewcache:YES];
[yellowViewController viewWillAppear:YES];
[blueViewController viewWillDisappear:YES];
[yellowViewController.view removeFromSuperview];
[self.view insertSubview:blueViewController.viewatIndex:0];
[blueViewController viewDidDisappear:YES];
[yellowViewController viewDidAppear:YES];
}
}
- (void)viewDidUnload
{
[yellowViewController release];
[blueViewController release];
[superviewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
BlueViewController和YelloViewController的源代码
// BlueViewController.h
// test08
//
// Created by lily on 12-2-1.
// Copyright 2012年 lily. All rights reserved.
//
#import<UIKit/UIKit.h>
@interface BlueViewController :UIViewController {
}
- (IBAction) blueButtonPressed;
@end
// BlueViewController.m
// test08
//
// Created by lily bear on 12-2-1.
// Copyright 2012年 lily. All rights reserved.
//
#import"BlueViewController.h"
@implementation BlueViewController
- (IBAction)blueButtonPressed{
UIAlertView *alert = [[UIAlertViewalloc]
initWithTitle:@"Blue View Button Pressed"
message:@"You presssed the button on the blue view"
delegate:nil
cancelButtonTitle:@"Yep, I did."otherButtonTitles:nil];
[alert show];
[alert release];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
returnself;
}
- (void)dealloc
{
[superdealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
*/
- (void)viewDidUnload
{
[superviewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
//
// YellowViewController.h
// test08
//
// Created by lily bear on 12-2-1.
// Copyright 2012年 lily. All rights reserved.
//
#import<UIKit/UIKit.h>
@interface YellowViewController :UIViewController {
}
- (IBAction) yellowButtonPressed;
@end
//
// YellowViewController.m
// test08
//
// Created by lily bear on 12-2-1.
// Copyright 2012年 lily. All rights reserved.
//
#import"YellowViewController.h"
@implementation YellowViewController
- (IBAction)yellowButtonPressed{
UIAlertView *alert = [[UIAlertViewalloc]
initWithTitle:@"Yellow View Button Pressed"
message:@"You presssed the button on the yellow view"
delegate:nil
cancelButtonTitle:@"Yep, I did."otherButtonTitles:nil];
[alert show];
[alert release];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
returnself;
}
- (void)dealloc
{
[superdealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[superdidReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
*/
- (void)viewDidUnload
{
[superviewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end