根视图应用程序(两个视图之间的切换)
在IOS上做多视图的切换是个比较重要的东西,也是一个要重点了解的知识点。在iOS下开发的程序基本上所有的都是有多个视图,下面我就带大家一起来创建一个多视图的应用
首先让我们看看实际运行的效果图
![](https://img-my.csdn.net/uploads/201301/12/1357971062_2120.png)
![](https://img-my.csdn.net/uploads/201301/12/1357971075_2993.png)
![](https://img-my.csdn.net/uploads/201301/12/1357971095_8706.png)
![](https://img-my.csdn.net/uploads/201301/12/1357971106_1435.png)
通过点击下面的切换视图按钮来达到视图切换的目的
目录大纲
![](https://img-my.csdn.net/uploads/201301/12/1357971170_5462.png)
创建两个不同的视图
第一步:选择Group点击右健选择 new File
![](https://img-my.csdn.net/uploads/201301/12/1357971409_7026.png)
第二步:选择iOS下面的CocoaTouch然后是Object_C 点击下一步
![](https://img-my.csdn.net/uploads/201301/12/1357971480_6557.png)
第三步:创建 点击下一步完成
![](https://img-my.csdn.net/uploads/201301/12/1357971638_4160.png)
Class写一个自己的类明
SubClass of 中有很多的选择项,这里我们要选择UIViewController这个对象
注意:记得钩上With XIB for user interface 选项
下面就只要设计界面,然后完善视图就OK啦
只要按照下面的代码然后执行就可以看到上面的效果
ViewController.h
注意在主视图的H文件中要记得引入两个子视图控件的H文件
#import "YellowViewController.h"
#import "BlueViewController.h"
@interface ViewController : UIViewController {
YellowViewController *yellowViewController;
BlueViewController *blueViewController;
}
@property(nonatomic,retain)YellowViewController *yellowViewController;
@property(nonatomic,retain)BlueViewController *blueViewController;
//定义按钮切换视图
-(IBAction)switchViews:(id)sender;
@end
ViewController.m
@implementation ViewController
@synthesize yellowViewController;
@synthesize blueViewController;
//定义按钮切换视图
-(IBAction)switchViews:(id)sender{
//替换相互的视图
//第一个判断是判断黄色视图在主视图中是否存在
if(self.yellowViewController.view.superview == nil){
//判断当前现实的是否为黄色的视图
if(self.yellowViewController.view == nil){
YellowViewController *yellowViewCV = [[YellowViewController alloc] initWithNibName:@"YellowViewController" bundle:nil];
self.yellowViewController = yellowViewCV;
[yellowViewCV release];
}
//移除掉蓝色视图
[self.blueViewController.view removeFromSuperview];
[self.view insertSubview:yellowViewController.view atIndex:0];
}else{
if(self.blueViewController.view == nil){
BlueViewController *blueViewCV = [[BlueViewController alloc] initWithNibName:@"BlueViewController" bundle:nil];
self.blueViewController = blueViewCV;
[blueViewCV release];
}
[self.yellowViewController.view removeFromSuperview];
[self.view insertSubview:blueViewController.view atIndex:0];
}
}
//让程序一加载的时候就调用蓝色视图
- (void)viewDidLoad
{
[super viewDidLoad];
BlueViewController *blueViewCV = [[BlueViewController alloc] initWithNibName:@"BlueViewController" bundle:nil];
self.blueViewController = blueViewCV;
//为什么这里创建号成员变量获取视图后有release呢?原因在于要释放资源,因为在H文件中该成员变量定义的为 @property(nonatomic,retain) 中的retain类型,该类型可以保持成员变量
[blueViewCV release];
//最后再把这个视图插入到主视图当中去 注意后面一个参数为0是为了是视图放到最地层
[self.view insertSubview:blueViewController.view atIndex:0];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc
{
[yellowViewController release];
[blueViewController release];
[super dealloc];
}
@end
YellowViewController.h
@interface YellowViewController : UIViewController
-(IBAction)yellclick:(id)sender;
@end
YellowViewController.m
@implementation YellowViewController
-(IBAction)yellclick:(id)sender{
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"紧急提示"
message:@"手真贱,乱摸,还想不想活."
delegate:nil
cancelButtonTitle:@"快放手"
otherButtonTitles:@"我就要摸", nil];
[alert show];
[alert release];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
BlueViewController.h
@interface BlueViewController : UIViewController
-(IBAction)blueclick:(id)sender;
@end
BlueViewController.m
@implementation BlueViewController
-(IBAction)blueclick:(id)sender{
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"紧急提示"
message:@"亲爱的我想你."
delegate:nil
cancelButtonTitle:@"想你妹"
otherButtonTitles:@"我也想你", nil];
[alert show];
[alert release];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
这样一来我们的多视图应用程序就完成啦。