ios笔记(1):pushviewcontroller、presentviewcontroller 简单区分

pushViewController

一般在UINavigationController的时候用,

[self.navigationController pushViewController:viewController animated:YES];

返回之前的视图是:

[self.navigationController popViewControllerAnimated:YES];

push以后会在navigationleft bar自动添加back按钮,它的响应方法就是返回。所以一般不需要写返回方法,点back按钮即可。

presentViewController

其它时候使用presentViewController

[self presentViewController:viewController animated:YES completion:nil];

返回之前的视图是:

[self dismissViewControllerAnimated:YES completion:nil];

附加

使用presentViewController,从A->B->C,从C直接返回A。

1
2
3
4
5
// C
- (void)back {
  [self dismissViewControllerAnimated:YES completion:nil];    // 注意一定是NO!  
  [[NSNotificationCenter defaultCenter] postNotificationName:@"backback" object:nil];
}
1
2
3
4
5
6
7
8
// B
- (void)viewDidLoad {
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(back) name:@"backback" object:nil];
}

- (void)back {
  [self dismissViewControllerAnimated:YES completion:nil];
}