共同点:
present和朴实方法都可以用于推出新的界面,present和dismiss对应使用,push和pop对应使用。
不同点:
present弹出的视图是模态视图(是一个临时视图)并且模式情况下不是全屏显示的;而push由视图栈控制,每一个视图都入栈,调用之前的视图则需要出栈。
present只能逐级返回;而push可以返回任意一层。
使用方法:
使用UINavigationController时使用push方法,代码如下:
[self.navigationController pushViewController:xxx animated:BOOL];
返回时使用pop方法,代码如下:
[self.navigationController popViewControllerAnimated:BOOL];
不使用导航栏的时候使用present方法,代码如下:
[self presentViewController:xxx animated:BOOL completion:nil];
返回时使用dismiss方法,代码如下:
[self dismissViewControllerAnimated:BOOL completion:nil];
下面我们用一个小小的demo来实际使用这几种方法,流程如下:
(1)A视图中创建一个按钮点击后present到B视图。
(2)B视图中创建两个按钮,一个点击后present到C视图,一个点击后push到C视图。
(3)C视图中创建两个按钮,一个点击后dismiss到B视图,一个点击后pop到B视图。
代码如下:
A视图(ViewController)present到B视图(SecondViewController):
#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor orangeColor];
UIButton *testButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[testButton setTitle:@"跳到B界面" forState:UIControlStateNormal];
[testButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
testButton.frame = CGRectMake([UIScreen mainScreen].bounds.size.width / 3, [UIScreen mainScreen].bounds.size.height / 3, [UIScreen mainScreen].bounds.size.width / 4, 50);
[testButton addTarget:self action:@selector(pressButton) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:testButton];
}
//按钮的点击事件
- (void) pressButton {
NSLog(@"pressButton TO B");
SecondViewController *second = [[SecondViewController alloc] init];
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:second];
navigation.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:navigation animated:YES completion:nil];
}
@end
B视图(SecondViewController)的两个跳转到C视图(ThirdViewController)的按钮:
#import "SecondViewController.h"
#import "ThirdViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor yellowColor];
UIButton *testButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[testButton setTitle:@"点击跳转C界面nav" forState:UIControlStateNormal];
[testButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[testButton addTarget:self action:@selector(pressButton) forControlEvents:UIControlEventTouchUpInside];
testButton.frame = CGRectMake([UIScreen mainScreen].bounds.size.width / 4, [UIScreen mainScreen].bounds.size.height / 4, [UIScreen mainScreen].bounds.size.width / 2, 50);
[self.view addSubview:testButton];
UIButton *testButtonSecond = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[testButtonSecond setTitle:@"点击跳转C界面present" forState:UIControlStateNormal];
[testButtonSecond setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[testButtonSecond addTarget:self action:@selector(pressButtonSecond) forControlEvents:UIControlEventTouchUpInside];
testButtonSecond.frame = CGRectMake([UIScreen mainScreen].bounds.size.width / 4, [UIScreen mainScreen].bounds.size.height / 3, [UIScreen mainScreen].bounds.size.width / 2, 50);
[self.view addSubview:testButtonSecond];
}
//push到C界面
- (void) pressButton {
NSLog(@"pressButtonPush");
ThirdViewController *third = [[ThirdViewController alloc] init];
third.modalPresentationStyle = UIModalPresentationFullScreen;
[self.navigationController pushViewController:third animated:YES];
}
//present到C界面
- (void) pressButtonSecond {
NSLog(@"pressButtonPresent");
ThirdViewController *third = [[ThirdViewController alloc] init];
third.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:third animated:YES completion:nil];
}
@end
C视图(ThirdViewController)的两个返回按钮:
#import "ThirdViewController.h"
@interface ThirdViewController ()
@end
@implementation ThirdViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
UIButton *testButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[testButton setTitle:@"back With pop" forState:UIControlStateNormal];
[testButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[testButton addTarget:self action:@selector(pressButton) forControlEvents:UIControlEventTouchUpInside];
testButton.frame = CGRectMake([UIScreen mainScreen].bounds.size.width / 4, [UIScreen mainScreen].bounds.size.height / 4, [UIScreen mainScreen].bounds.size.width / 2, 50);
[self.view addSubview:testButton];
UIButton *testButtonSecond = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[testButtonSecond setTitle:@"back With dismiss" forState:UIControlStateNormal];
[testButtonSecond setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[testButtonSecond addTarget:self action:@selector(pressButtonSecond) forControlEvents:UIControlEventTouchUpInside];
testButtonSecond.frame = CGRectMake([UIScreen mainScreen].bounds.size.width / 4, [UIScreen mainScreen].bounds.size.height / 3, [UIScreen mainScreen].bounds.size.width / 2, 50);
[self.view addSubview:testButtonSecond];
}
//使用pop返回
- (void) pressButton {
NSLog(@"backpop");
[self.navigationController popViewControllerAnimated:YES];
}
//使用dismiss返回
- (void) pressButtonSecond {
NSLog(@"backdismiss");
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
运行结果如下:
当我们使用导航栏从B视图push到C视图,再点击第一个按钮(用pop方法返回):
C视图界面如下:
点击第一个按钮后结果:
我们跳转到了B界面
但是当我们在C视图界面点击第二个按钮(用dismiss方法返回)后的运行结果:
我们返回到了A视图界面
当我们从B视图界面点击第二个按钮(用present方法跳转到C视图)后再点击第一个按钮(用pop方法返回)的结果如下:
我们可以看到,点击后并没有发生界面跳转,当前界面还是C视图界面,这是因为pop方法是导航栏才能使用的方法,我们没有使用导航栏的push方法而使用了present方法,结果是C视图界面中就没有导航栏,也就无法使用pop方法。但是此时我们点击C视图界面的第二个按钮(用dismiss方法返回)后的结果如下:
我们跳转到了B视图界面。