iphone视图间跳转之一:自定义视图控制器

           本文将介绍两个视图之间切换,在介绍它之间,我们先理解几个概念。
我们在创建的每个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


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值