IOS中UIViewController切换

假设现在存在两个UIViewController,即FirstUIViewController和SecondUIViewController

1
2
3
FirstUIViewController *first = [[[FirstUIViewController alloc] init] autorelease];

SecondUIViewController *second = [[[SecondUIViewController alloc] init] autorelease];

①:存在NavigationController

方法一:运用NavigationController中的pushViewController:animated:消息

FirstUIViewController.m中执行:

1
[self.navigationController pushViewController:second animated:YES];

如果需要返回原来的UIViewController:

1
[self.navigationController popViewControllerAnimated:YES];

方法二:运用UIViewController中的presentModalViewController:animated:消息

FirstUIViewController.m中执行:

1
[self.navigationController presentModalViewController:second animated:YES];

如果需要返回原来的UIViewController:

1
[self.navigationController dismissModalViewControllerAnimated:YES];

②:不存在NavigationController

FirstUIViewController.m中执行:

1
[self presentModalViewController:second animated:YES];

如果需要返回原来的UIViewController:

1
[self dismissModalViewControllerAnimated:YES];

PS:在使用presentModalViewController:animated:消息的时候可以设计动画效果

1
2
3
second.modalTransitionStyle = UIModalTransitionStylePartialCurl;

[self presentModalViewController:second animated:YES];

具体设计的参数如下:

UIModalTransitionStyleCoverVertical

UIModalTransitionStyleFlipHorizontal

UIModalTransitionStyleCrossDissolve

UIModalTransitionStylePartialCurl

Tips:在IOS6中presentModalViewController:animated:消息已经过时了,可以用

presentViewController: animated:completion:进行替换,同样dismissModalViewControllerAnimated:用

dismissViewControllerAnimated:completion:进行替换

文章目录
  1. 1. ①:存在NavigationController
    1. 1.1. 方法一:运用NavigationController中的pushViewController:animated:消息
    2. 1.2. 方法二:运用UIViewController中的presentModalViewController:animated:消息
  2. 2. ②:不存在NavigationController
,