本文将介绍两个视图之间切换,在介绍它之间,我们先理解几个概念。
我们在创建的每个View-based Application程序的都会生成一个XXXAppDelegate的头文件与源文件。
如下:
#import <UIKit/UIKit.h>
@class Hello_WorldViewController;
@interface Hello_WorldAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
Hello_WorldViewController *viewController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet Hello_WorldViewController *viewController;
@end
从上面可以看出它拥有一个UIWindow、以及XXXXViewController的成员。UIWindow可以认为它是一个iphone无任何内容的窗口,XXXXViewController是程序第一次加载到UIWindow需要显示的视图控制器,即根视图控制器,里面可以放其它视图,作为其它视图的跟视图控制器。
下面我们写个测试来说明这一切,先看效果图:
主要代码:
#import <UIKit/UIKit.h>
#import "BlueViewController.h"
#import "YellowViewController.h"
@interface MutilViewSwitchViewController : UIViewController {
YellowViewController *yellowViewController;
BlueViewController *blueViewController;
}
@property (nonatomic, retain) YellowViewController *yellowViewController;
@property (nonatomic, retain) BlueViewController *blueViewController;
-(IBAction) switchViews:(id)sender;
@end
#import "MutilViewSwitchViewController.h"
@implementation MutilViewSwitchViewController
@synthesize yellowViewController;
@synthesize blueViewController;
-(IBAction) switchViews:(id)sender {
if (self.yellowViewController.view.superview == nil) {
if (self.yellowViewController.view == nil) {
YellowViewController *yellowController = [[YellowViewController alloc]
initWithNibName:@"YellowViewController" bundle:nil];
self.yellowViewController = yellowController;
[yellowController release];
}
[blueViewController.view removeFromSuperview];
[self.view insertSubview:yellowViewController.view atIndex:0];
} else {
if (self.blueViewController.view == nil) {
BlueViewController *blueController = [[BlueViewController alloc]
initWithNibName:@"BlueViewController" bundle:nil];
self.blueViewController = blueController;
[blueController release];
}
[yellowViewController.view removeFromSuperview];
[self.view insertSubview:blueViewController.view atIndex:0];
}
}
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
*/
/*
// 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 {
BlueViewController *blueController = [[BlueViewController alloc]
initWithNibName:@"BlueViewController" bundle:nil];
self.blueViewController = blueController;
[self.view insertSubview:blueController.view atIndex:0];
[blueController release];
[super viewDidLoad];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (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.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.yellowViewController = nil;
self.blueViewController = nil;
}
- (void)dealloc {
[yellowViewController release];
[blueViewController release];
[super dealloc];
}
@end