Overview

近几天在测试公司项目时,遇到一个UISearchController崩溃的问题。

Terminating app due to uncaught exception 'NSInvalidArgumentException', 
reason: 'Application tried to present modal view controller on itself. 
Presenting controller is .'

崩溃的原因

问题似乎是iOS11之后,系统试图恢复firstResponder状态。

解决办法

在关闭视图之前,设置searchController的active为NO,例如:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   self.searchController.active = NO; // Add this !
   ...

   [self dismissViewControllerAnimated:YES completion:nil];
}

参考资料

iOS UISearchController crash: Application tried to present modal view controller on itself