工程目录结构如下图所示(ios5 ARC + Xcode4.4):
1。添加BlueViewController
2。添加YellowViewController
3。ViewController.xib中添加一个按钮 ,按钮标题为:change views,
ViewController.h中添加代码
#import <UIKit/UIKit.h>
@class BlueViewController;
@class YellowViewController;
@interface ViewController : UIViewController
{
BlueViewController *blueViewController;
YellowViewController *yellowViewController;
}
@property (strong, nonatomic) BlueViewController *blueViewController;
@property (strong, nonatomic) YellowViewController *yellowViewController;
- (IBAction)showBlueView:(UIButton *)sender;
@end
4。在ViewController.m中添加代码
//
// ViewController.m
// test
//
// Created by on 12-9-13.
// Copyright (c) 2012年 All rights reserved.
//
#import "ViewController.h"
#import "BlueViewController.h"
#import "YellowViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize blueViewController;
@synthesize yellowViewController;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//BlueViewController *blueController = [[BlueViewController alloc] initWithNibName: @"BlueViewController" bundle: nil];
//self.blueViewController = blueController;
//[self.view insertSubview: blueController.view atIndex: 0];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (IBAction)showBlueView:(UIButton *)sender
{
[UIView beginAnimations: @"view flip" context: nil];
[UIView setAnimationDuration: 1.25];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
if (self.yellowViewController.view.superview == nil)
{
if (self.yellowViewController == nil) {
YellowViewController *yellowController = [[YellowViewController alloc] initWithNibName: @"YellowViewController" bundle: nil];
self.yellowViewController = yellowController;
}
[UIView setAnimationTransition: UIViewAnimationTransitionCurlUp forView: self.view cache: YES];
[yellowViewController viewWillAppear: YES];
[blueViewController viewWillDisappear: YES];
[blueViewController.view removeFromSuperview];
[self.view insertSubview: yellowViewController.view atIndex: 0];
[blueViewController viewDidDisappear: YES];
[yellowViewController viewDidAppear: YES];
}
else
{
if (self.blueViewController == nil)
{
BlueViewController *blueController = [[BlueViewController alloc] initWithNibName: @"BlueViewController" bundle: nil];
self.blueViewController = blueController;
}
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView: self.view cache: YES];
[blueViewController viewWillAppear: YES];
[yellowViewController viewWillDisappear: YES];
[yellowViewController.view removeFromSuperview];
[self.view insertSubview: blueViewController.view atIndex: 0];
[yellowViewController viewDidDisappear: YES];
[blueViewController viewDidAppear: YES];
}
[UIView commitAnimations];
}
@end
5。BlueViewController.xib中添加一个按钮,响应UIAlertView。
BlueViewController.h中添加按钮函数声明
#import <UIKit/UIKit.h>
@interface BlueViewController : UIViewController
- (IBAction)blueButtonPressed:(UIButton *)sender;
@end
6。BlueViewController.m中添加函数实现
- (IBAction)blueButtonPressed:(UIButton *)sender
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"蓝色按钮标题" message: @"你点击了蓝色视图上的按钮" delegate: nil cancelButtonTitle: @"yep, I did." otherButtonTitles: nil, nil];
[alert show];
}
7。YellowViewController.xib中添加一个按钮,响应UIAlertView。
YellowViewController.h中添加按钮函数声明
#import <UIKit/UIKit.h>
@interface YellowViewController : UIViewController
- (IBAction)yellowButtonPressed:(UIButton *)sender;
@end
8。YellowViewController.m中添加按钮函数实现
- (IBAction)yellowButtonPressed:(UIButton *)sender
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"yellow title" message: @"message,.abc,asdf" delegate: nil cancelButtonTitle: @"ok" otherButtonTitles: nil, nil];
[alert show];
}