涉及知识点
1. the root controller
它是一个当应用加载的时候用到的控制器。
这个root controller通常是UINavigationController和UITabBarController的实例或者是一个客户定制过的UIViewController的子类的实例。
2. content view
content view 是我们放置显示控件的地方。它有自己的controller。每个content view都是一个UIView或者是它的子类。
3. scene场景
在storyboard中,每个scene都有一个content view和它的controller
4. lazy loading
懒加载,在需要的时候在加载需要的内容。
小例子实现代码如下:
这个例子有点过于简单。而且依赖于storyboard。
#import "ViewController.h"
#import "BlueViewController.h"
#import "YellowViewController.h"
@interface ViewController ()
@property (nonatomic, strong) YellowViewController *yellowViewController;
@property (nonatomic, strong) BlueViewController *blueViewController;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.blueViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Blue"];
[self.view insertSubview:_blueViewController.view atIndex:0];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
if (!self.blueViewController.view.superview) {
self.blueViewController = nil;
}else{
self.yellowViewController = nil;
}
}
- (IBAction)switchViews:(id)sender{
[UIView beginAnimations:@"View Flip" context:NULL];
[UIView setAnimationDuration:0.4];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
if (!self.yellowViewController.view.superview) {
if (!self.yellowViewController) {
self.yellowViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Yellow"];
}
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
[self.blueViewController.view removeFromSuperview];
[self.view insertSubview:self.yellowViewController.view atIndex:0];
}else{
if (!self.blueViewController) {
self.blueViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Blue"];
}
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
[self.yellowViewController.view removeFromSuperview];
[self.view insertSubview:self.blueViewController.view atIndex:0];
}
[UIView commitAnimations];
}