一.定义区别

  1. pushViewController 导航栏控制器入栈的方式切换页面(pop可以返回任意一层)push一般用于同一业务不同界面之间的切换(也就是只能导航栏间的切换,从第二个导航栏到第三个导航栏)
- (void) pass {
    NSLog(@"pass to third");
    //第三个同样是导航栏,而且两个导航栏之间可以自由返回
    ThirdViewController *thirdVC = [[ThirdViewController alloc] init];
    [self.navigationController pushViewController:thirdVC animated:YES];
}
  1. presentViewController模式切换的方式切换页面(dismiss只能逐级返回)present一般用于不同业务界面的切换。
    若是由视图页面转到导航栏,要先建立一个导航栏。
//出来一个navigation
    SecondViewController *secondVC = [[SecondViewController alloc] init];
    UINavigationController *navSecondVC = [[UINavigationController alloc] initWithRootViewController:secondVC];
    navSecondVC.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:navSecondVC animated:YES completion:nil];

若是两个视图间,直接用present即可

- (void) press {
    NSLog(@"press 01");
    SecondViewController *secondVC = [[SecondViewController alloc] init];
    secondVC.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:secondVC animated:YES completion:nil];
}

二. 返回方法

  1. push和pop对应
    用UINavigationController的时候用pushViewController:animated返回之前的视图[[self navigationController] popViewControllerAnimated:YES]; push以后会在navigation的left bar自动添加back按钮,所以一般不用写返回,点back方法即可
    有关pop,pop一般返回一级和左上角的back的作用是一样的,所以它的用处在于可以分级返回
//返回上一视图
	[self.navigationController popViewControllerAnimated:YES];

	//返回根视图(to root)
	[self.navigationController popToRootViewControllerAnimated:YES];

	//返回任意视图(atindex后加返回的层数)
    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
  1. present和dismiss对应presentModalViewController:animated
    [self presentModalViewController:controller animated:YES];
    返回之前的视图 [self dismissModalViewControllerAnimated:YES];
    dismiss在导航栏和页面中都可以使用,在页面中即返回上一级页面,在导航栏中使用的话是返回到所有导航栏前的一个页面,但是写法是相同的。
- (void) backDiss {
    NSLog(@"backdiss");
    [self dismissViewControllerAnimated:YES completion:nil];
}

三.使用区别

  1. [self presentModalViewController:controller animated:YES];主语是UIVIewController,即单纯页面切换
  2. [self.navigationController pushViewController:upViewController animated:YES];与[self.navigationController popViewControllerAnimated:YES];这两句时主语是navigationController,所以如果是在导航栏上实现页面的跳转使用push,除此之外的都是用present

代码

2020-10-04 23:15:46.330677+0800 present和push[79292:5066460] press 01 to
2020-10-04 23:16:19.915102+0800 present和push[79292:5066460] pass to third
2020-10-04 23:16:42.522594+0800 present和push[79292:5066460] backPop to Second
2020-10-04 23:16:47.412832+0800 present和push[79292:5066460] pass to third
2020-10-04 23:16:48.862871+0800 present和push[79292:5066460] backdiss to First(dissmiss因为上一级页面是第一个,两个导航栏是同一级页面)
2020-10-04 23:17:01.976698+0800 present和push[79292:5066460] press 01 to
2020-10-04 23:17:04.255234+0800 present和push[79292:5066460] pass to four
2020-10-04 23:17:12.925467+0800 present和push[79292:5066460] back2 ?->dissmiss second(因为上一级页面是第二个)
2020-10-04 23:18:01.000129+0800 present和push[79292:5071067] [ServicesDaemonManager] interruptionHandler is called. -[FontServicesDaemonManager connection]_block_invoke

我写的代码,第一个视图present到第二个(导航栏)。第二个push到第三个(导航栏),第三个可以通过dismiss返回到第一个,也可以通过pop回到第二个。第二个还可以present到第四个(一个界面),第四个通过dismiss回到第二个(导航栏)。如下图:

ios 先present 在push ios push present 区别_ios 先present 在push


ios 先present 在push ios push present 区别_ios 先present 在push_02


ios 先present 在push ios push present 区别_入栈_03


ios 先present 在push ios push present 区别_入栈_04