SwitchViewController 负责在按下Switch Views 时切换 BlueViewController 和 YellowViewController。
控制器类 和 对应的 .xib文件 都是单独创建。手动组建
效果图:
1、新建 Empty Application 。不选 “Use Core Data”
2、新建 SwitchViewController 类和 SwitchViewController.xib
BlueViewController 类和 BlueView.xib
YellowViewController 类和 YellowView.xib
3、编写SwitchViewController 类
1 #import <UIKit/UIKit.h> 2 3 @class BlueViewController; 4 @class YellowViewController; 5 6 @interface SwitchViewController : UIViewController 7 8 @property (strong, nonatomic) BlueViewController *blueViewController; 9 @property (strong, nonatomic) YellowViewController *yellowViewController; 10 11 -(IBAction)switchViews:(id)sender; 12 13 @end
1 #import "SwitchViewController.h" 2 #import "BlueViewController.h" 3 #import "YellowViewController.h" 4 5 @interface SwitchViewController () 6 7 @end 8 9 @implementation SwitchViewController 10 11 @synthesize blueViewController = _blueViewController; 12 @synthesize yellowViewController = _yellowViewController; 13 14 -(IBAction)switchViews:(id)sender 15 { 16 [UIView beginAnimations:@"View Flip" context:nil]; 17 [UIView setAnimationDuration:1.25]; 18 [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 19 20 if (self.yellowViewController.view.superview == nil) 21 { 22 if (self.yellowViewController == nil) 23 { 24 YellowViewController *yellowViewController = [[YellowViewController alloc] initWithNibName:@"YellowView" bundle:nil]; 25 self.yellowViewController = yellowViewController; 26 } 27 [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES]; 28 [self.blueViewController viewWillAppear:YES]; 29 [self.yellowViewController viewWillDisappear:YES]; 30 31 [self.blueViewController.view removeFromSuperview]; 32 [self.view insertSubview:self.yellowViewController.view atIndex:0]; 33 34 [self.yellowViewController viewWillDisappear:YES]; 35 [self.blueViewController viewWillAppear:YES]; 36 } 37 else 38 { 39 if (self.blueViewController == nil) 40 { 41 BlueViewController *blueViewController = [[BlueViewController alloc] initWithNibName:@"BlueView" bundle:nil]; 42 self.blueViewController = blueViewController; 43 } 44 45 [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES]; 46 [self.yellowViewController viewWillAppear:YES]; 47 [self.blueViewController viewWillDisappear:YES]; 48 49 [self.yellowViewController.view removeFromSuperview]; 50 [self.view insertSubview:self.blueViewController.view atIndex:0]; 51 52 [self.blueViewController viewWillDisappear:YES]; 53 [self.yellowViewController viewWillAppear:YES]; 54 } 55 [UIView commitAnimations]; 56 } 57 58 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 59 { 60 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 61 if (self) { 62 // Custom initialization 63 } 64 return self; 65 } 66 67 - (void)viewDidLoad 68 { 69 BlueViewController *blueViewController = [[BlueViewController alloc] initWithNibName:@"BlueView" bundle:nil]; 70 self.blueViewController = blueViewController; 71 [self.view insertSubview:blueViewController.view atIndex:0]; 72 73 [super viewDidLoad]; 74 // Do any additional setup after loading the view. 75 } 76 77 - (void)viewDidUnload 78 { 79 [super viewDidUnload]; 80 // Release any retained subviews of the main view. 81 } 82 83 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 84 { 85 return (interfaceOrientation == UIInterfaceOrientationPortrait); 86 } 87 88 - (void)didReceiveMemoryWarning 89 { 90 if (self.blueViewController.view.superview == nil) 91 self.blueViewController = nil; 92 else 93 self.yellowViewController = nil; 94 } 95 96 @end
4、关联SwitchViewController类:
5、为 SwitchViewController.xib 添加 “ToolBar”,修改 “Bar Button Item” 为 “Switch Views”
为 “Bar Button” 添加事件关联到 -(IBAction)switchViews:(id)sender;
关联控制器的Outlet “view” 到 视图 “view”
6、设置 “BlueView.xib”:
关联BlueViewController类
添加背景颜色,设置视图属性
添加 “Round Rect Button”,添加事件:
关联控制器的Outlet “view” 到 视图 “view”
7、修改文件
1 #import <UIKit/UIKit.h> 2 3 @interface BlueViewController : UIViewController 4 5 - (IBAction)blueButtonPressed:(id)sender; 6 7 @end
1 #import "BlueViewController.h" 2 3 @interface BlueViewController () 4 5 @end 6 7 @implementation BlueViewController 8 9 - (IBAction)blueButtonPressed:(id)sender 10 { 11 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Blue View Button Pressed" 12 message:@"You pressed the button on the blue view" 13 delegate:nil 14 cancelButtonTitle:@"Yep, I did." 15 otherButtonTitles:nil]; 16 [alert show]; 17 } 18 19 // 省略其他方法 20 21 @end
8、设置“YellowView.xib” 和修改 YellowViewController 类 分别同6,7。
9、修改 AppDelegate 类
1 @class SwitchViewController; 2 3 @interface AppDelegate : UIResponder <UIApplicationDelegate> 4 5 @property (strong, nonatomic) UIWindow *window; 6 @property (strong, nonatomic) SwitchViewController *switchViewController; 7 8 @end
1 #import "AppDelegate.h" 2 #import "SwitchViewController.h" 3 4 @implementation AppDelegate 5 6 @synthesize window = _window; 7 @synthesize switchViewController = _switchViewController; 8 9 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 10 { 11 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 12 // Override point for customization after application launch. 13 self.switchViewController = [[SwitchViewController alloc] initWithNibName:@"SwitchViewController" bundle:nil]; 14 self.window.rootViewController = self.switchViewController; 15 [self.window makeKeyAndVisible]; 16 return YES; 17 } 18 19 @end
。