例如有三个页面,A,B,C,分别都用PresentViewController推送,A-> B-> C, 如果想要从C返回A页面,可以如下解决:
在C页面中:
[self dismissViewControllerAnimated:YES completion:^{}];
[[NSNotificationCenter defaultCenter] postNotificationName:@"closeB" object:nil];
在B页面中:
viewDidLoad方法添加:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(closeView) name:@"closeB" object:nil];
然后在BViewController中添加selector监听方法:
-(void)closeView{
[self dismissViewControllerAnimated:YES completion:^{}];
}
然而可能出现的问题是:当D->C->B->A的时候,可能在B页面中出现主程序没有加载完毕B页面时,系统要求关闭B,会报错:
Warning: Attempt to dismiss from view controller <xxxxxxxxxController:
解决方法是在closeView方法中添加下面代码:dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.51*NSEC_PER_SEC)),
dispatch_get_main_queue,^{
if (![self.presentedViewController isBeingDismissed]){
[self dismissViewControllerAnimated:YES completion:^{}];
}
});