ios5中UIViewController addChildViewController等新方法

原文地址:http://justsee.iteye.com/blog/1829687 


ios4还是3,以前基于在vviewcontroller上的切换,基本上就2中方式,导航控制器上的push,或者普通控制器上modal,现在终于多了一个这种玩意儿了。在ios5中,ViewController中新添加了下面几个方法:

addChildViewController:

removeFromParentViewController

transitionFromViewController:toViewController:duration:options:animations:completion:

willMoveToParentViewController:

didMoveToParentViewController:

下面详细介绍一下addChildViewController,一个ViewController可以添加多个子ViewController,但是这些子ViewControlle要想显示出来,需要把子controller的视图添加到父视图的相应位置r,可以通过transitionFromViewController:toViewController:duration:options:animations:completion:这个方法转换显示的子视图。同时加入相应的动画。下面以一个例子来说明这几个新方法:

下面详细介绍一下上述效果的实现:

创建项目,changeViewController。

添加相应的viewController,MainViewController、FirstViewController、SecondViewController、ThirdViewController。

3.把MainViewController添加到window中。

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

{

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

// Override point for customization after application launch.

MainViewController *mainViewController=[[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];

self.window.rootViewController=mainViewController;

[self.window makeKeyAndVisible];

return YES;

}

4.在MainViewController中添加三个按钮,并且连接onClickbutton方法。

5.在MainViewController中添加三个子controller

#pragma mark – View lifecycle

- (void)viewDidLoad

{

[super viewDidLoad];

// Do any additional setup after loading the view from its nib.

FirstViewController *firstViewController=[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];

[self addChildViewController:firstViewController];

SecondViewController *secondViewController=[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

[self addChildViewController:secondViewController];

ThirdViewController *thirdViewController=[[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];

[self addChildViewController:thirdViewController];

[contentView addSubview:thirdViewController.view];

currentViewController=thirdViewController;

}

其中要把其中的一个子controller的view添加到根视图中,这样才能显示出相应的视图。

6.点击按钮,切换视图。

-(IBAction)onClickbutton:(id)sender

{

FirstViewController *firstViewController=[self.childViewControllers objectAtIndex:0];

ThirdViewController *thirdViewController=[self.childViewControllers objectAtIndex:2];

SecondViewController *secondViewController=[self.childViewControllers objectAtIndex:1];

if ((currentViewController==firstViewController&&[sender tag]==1)||(currentViewController==secondViewController&&[sender tag]==2) ||(currentViewController==thirdViewController&&[sender tag]==3) ) {

return;

}

UIViewController*oldViewController=currentViewController;

switch ([sender tag]) {

case 1:

{

NSLog(@"留言及回复");

[self transitionFromViewController:currentViewController toViewController:firstViewController duration:4 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{

}  completion:^(BOOL finished) {

if (finished) {

currentViewController=firstViewController;

}else{

currentViewController=oldViewController;

}

}];

}

break;

case 2:

{

NSLog(@"生日提醒");

[self transitionFromViewController:currentViewController toViewController:secondViewController duration:1 options:UIViewAnimationOptionTransitionFlipFromTop animations:^{

}  completion:^(BOOL finished) {

if (finished) {

currentViewController=secondViewController;

}else{

currentViewController=oldViewController;

}

}];

}

break;

case 3:

{

NSLog(@"好友申请");

[self transitionFromViewController:currentViewController toViewController:thirdViewController duration:1 options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{

}  completion:^(BOOL finished) {

if (finished) {

currentViewController=thirdViewController;

}else{

currentViewController=oldViewController;

}

}];

}

break;

default:

break;

}

}

其中我把按钮设置成不同的tag了。

这时候点击按钮,就可以切换子视图了。

可以看到,这些view在没有使用时,是不会被load的,并且当有Memory Warning时,当前没有显示的view自动被unload掉了。

这样写的好处:

多个UIViewController之间切换可以添加动画

当内存警告的时候,可以把当前不是激活状态的ViewController内存释放。所以新的方法确实能有效地节省内存,也能方便地处理内存不足时的资源回收

可以把代码更好分开

thx google!

iOS5ViewCtrlerSample-master.zip(21.1 KB)

你可能感兴趣的:(ios5中UIViewController addChildViewController等新方法)