关于带有UISeachController 的页面跳转异常问题
首先,先说一下我碰到的这个坑,项目中用到了搜索功能。第一时间想到了UISearchController,然后就碰到了我要说的坑。UISearchController 的对象处于活跃状态的时候,diss控制器的时候,会出现程序崩溃现象。如图所示:
当点击取消时走diss 方法,如果 UISearchController 的对象失去了焦点 会出现程序崩溃,如果没有失去焦点,diss时不会diss当前控制器,diss掉的是UISearchController 的对象,会发现页面并没有跳转(reason: 'Application tried to present modal view controller on itself. Presenting controller is <UISearchController>
*** First throw call stack
)。原因是UISearchController 的对象处于活跃状态,在跳转前 让UISearchController 的对象处于不活跃状态下,跳转就可以。加如下的判断:
if (self.searchController.active) {
[self.searchController setActive:NO];
}
[self dismissViewControllerAnimated:YES completion:nil];
如果是pop 的方法跳转界面的话 ,在跳转页面前 也要让UISearchController 的对象处于不活跃状态,不然pop 回到上一个界面,UISearchController 的对象 依然存在pop 回的控制器上。如下代码:
if (self.searchController.active) {
[self.searchController setActive:NO];
}
[self.navigationController popViewControllerAnimated:YES];