IPhone多视图切换

时间:2022-03-09 16:11:34

处理IPhone多个view切换是我们常遇到的问题,接下来有个比较实用的方法:

而且还附有创建空项目,内存告急处理和动画效果的实现!

具体步骤:

1.创建一个空的项目,然后添加一个ViewController,勾选上with xib文件,点击到xib文件看看File's Owner属性是否是当前的ViewController,如果不是,手动填写一下,并将他的view属性拖向当前的view,view如果改成UIController,那么该view就具备事件

2.修改AppDelegate.h

@classDXWViewController;

@property (strong, nonatomic) DXWViewController *viewController;

3.修改AppDelegate.m

首先在dealloc中添加

[_viewControllerrelease];

接着是添加

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

self.window = [[[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]] autorelease];

self.viewController = [[[DXWViewControlleralloc] initWithNibName:@"DXWViewController"bundle:nil] autorelease];

self.window.rootViewController = self.viewController;

[self.windowmakeKeyAndVisible];

returnYES;

}

上面是讲从0开始创建一个启动项目,下面是做一个多视图切换的功能

4.截下来是创建三个ViewController,分别是RedViewController(主view),YellowViewController,BlueViewController

IPhone多视图切换IPhone多视图切换IPhone多视图切换

主view下面是一个toolbar控件,其他两个view下面是在属性ButtonBar选择ToolBar实现

5.在主view中编码实现

ViewController.h:

#import <UIKit/UIKit.h>
#import "BlueViewController.h"
#import "YellowViewController.h"
@interface DXWViewController : UIViewController
@property(retain,nonatomic) BlueViewController *blueViewController;
@property(retain,nonatomic)YellowViewController *yellowViewController;
- (IBAction)blueClick:(id)sender;
- (IBAction)yellowClick:(id)sender;
- (IBAction)redClick:(id)sender;
- (IBAction)click:(id)sender; @end

ViewController.m:

#import "DXWViewController.h"

@interface DXWViewController ()

@end

@implementation DXWViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
} - (void)viewDidLoad
{
[super viewDidLoad]; }
//如果内存警告,则调用这个方法,可以让你自己清掉一些不需要用的内存
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
NSLog(@"内存告急");
//如果内存警告则释放无用资源
if (self.blueViewController.view.superview != nil) {
[self.yellowViewController.view removeFromSuperview];
}
else if(self.yellowViewController.view.superview != nil){
[self.blueViewController.view removeFromSuperview];
}
}
- (IBAction)click:(id)sender {
// NSLog(@"hello");
[UIView beginAnimations:@"anim" context:nil];
//一个动画的持续时间
[UIView setAnimationDuration:1.5f];
//一个动画的运动随时间快慢曲线
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//动画方式
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
//如果当前view的父view存在
if (self.blueViewController.view.superview != nil) {
//如果没创建则创建
if (self.yellowViewController == nil) {
self.yellowViewController = [[[YellowViewController alloc] initWithNibName:@"YellowViewController" bundle:nil] autorelease];
}
[self.blueViewController.view removeFromSuperview];
[self.view insertSubview:self.yellowViewController.view atIndex:10];
} else if (self.yellowViewController.view.superview != nil) {
[UIView beginAnimations:@"anim" context:nil];
//一个动画的持续时间
[UIView setAnimationDuration:2.5f];
//一个动画的运动随时间快慢曲线
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//动画方式
[UIView setAnimationTransition:UIViewAnimationOptionTransitionCurlUp forView:self.view cache:YES];
//如果没创建则创建
if (self.blueViewController == nil) {
self.blueViewController = [[[BlueViewController alloc] initWithNibName:@"BlueViewController" bundle:nil] autorelease];
}
[self.yellowViewController.view removeFromSuperview];
[self.view insertSubview:self.blueViewController.view atIndex:10];
[UIView commitAnimations];
}
//动画效果启动
[UIView commitAnimations];
}
- (IBAction)blueClick:(id)sender {
NSLog(@"blue");
//[self.blueViewController.view removeFromSuperview];
self.blueViewController = [[[BlueViewController alloc] initWithNibName:@"BlueViewController" bundle:nil] autorelease];
[self.view insertSubview:self.blueViewController.view atIndex:10];
} - (IBAction)yellowClick:(id)sender {
NSLog(@"yellow");
//加上这句就可以红黄蓝任意点击
//[self.yellowViewController.view removeFromSuperview];
self.yellowViewController = [[[BlueViewController alloc] initWithNibName:@"YellowViewController" bundle:nil] autorelease];
[self.view insertSubview:self.yellowViewController.view atIndex:10];
}
//主视图控制器
- (IBAction)redClick:(id)sender {
NSLog(@"red");
[self.yellowViewController.view removeFromSuperview];
[self.blueViewController.view removeFromSuperview];
} @end

旋转效果

IPhone多视图切换