iOS UISearchController 崩溃:应用程序试图在其自身上呈现模态视图控制器
iOS UISearchController crash: Application tried to present modal view controller on itself
根据 crashlytics 的说法,正在发生以下崩溃(很少)。
Application tried to present modal view controller on itself.
Presenting controller is .
我根本无法复制这个问题。这就是我设置 UISearch 控制器的方式。
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.delegate = self;
self.tableView.tableHeaderView = self.searchController.searchBar;
self.definesPresentationContext = YES;
感谢任何帮助,因为我完全没有想法。如果需要,我会 post 更多代码。
我在更新到 iOS 11 时遇到了这个问题。
我的场景是,我有一个文本字段,当用户开始编辑它时,一个 search-view,本质上是一个带有搜索栏的表格视图 header 弹出,一旦表格视图单元格被点击它应该关闭.
问题似乎是自 iOS 11 起,OS 尝试恢复 firstResponder 状态。长话短说。
当我将 active = NO 添加到我的 select 方法时,它很有帮助,就像这样
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.searchController.active = NO; // Add this !
...
[self dismissViewControllerAnimated:YES completion:nil];
}
如果您像我一样需要 searchController 在以模态方式呈现另一个控制器时保持 活动状态,则执行以下操作以获得与模态呈现相同的效果,而无需直接这样做:
快速说明:对 Obj-C 不够熟悉,无法给出答案,但这里有一个答案 Swift 4. 如有必要,有人可以随意编辑和添加 Obj-C,但我认为这里很清楚如何解决手头的问题,即使它在 Swift.
假设我有一个要弹出的菜单:
let info = the info you need to pass
let currVC = self.getTopMostViewController()
let menuVC = currVC.storyboard?.instantiateViewController(withIdentifier: "myStringIdentifierSetInStoryboard") as? EventMenuViewController
guard menuVC != nil else { return }
menuVC!.info = info // Pass info necessary (i.e. what you would normally pass in prepare(for segue: ...). menuVC.info is a global variable from your class
currVC.present(menuVC!, animated: true, completion: nil)
getTopMostViewController() 的实现可能会有所不同。我的在下面,改编自 .
func getTopMostViewController() -> UIViewController {
let anyVC = UIViewController()
if var topController = UIApplication.shared.keyWindow?.rootViewController {
while let presentedViewController = topController.presentedViewController {
topController = presentedViewController
}
return topController
}
return anyVC
}
希望对您有所帮助!这不会给您描述 iOS 12 和 Swift 4 的错误,尽管我在尝试使用活动搜索控制器以模态方式呈现时确实得到了那个确切的错误,这就是导致我来到这里的原因。
确保使用
self.searchController = UISearchController()
而不是
self.searchController = UISearchController(searchResultsController: self)
根据 crashlytics 的说法,正在发生以下崩溃(很少)。
Application tried to present modal view controller on itself. Presenting controller is .
我根本无法复制这个问题。这就是我设置 UISearch 控制器的方式。
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.delegate = self;
self.tableView.tableHeaderView = self.searchController.searchBar;
self.definesPresentationContext = YES;
感谢任何帮助,因为我完全没有想法。如果需要,我会 post 更多代码。
我在更新到 iOS 11 时遇到了这个问题。 我的场景是,我有一个文本字段,当用户开始编辑它时,一个 search-view,本质上是一个带有搜索栏的表格视图 header 弹出,一旦表格视图单元格被点击它应该关闭.
问题似乎是自 iOS 11 起,OS 尝试恢复 firstResponder 状态。长话短说。
当我将 active = NO 添加到我的 select 方法时,它很有帮助,就像这样
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.searchController.active = NO; // Add this !
...
[self dismissViewControllerAnimated:YES completion:nil];
}
如果您像我一样需要 searchController 在以模态方式呈现另一个控制器时保持 活动状态,则执行以下操作以获得与模态呈现相同的效果,而无需直接这样做:
快速说明:对 Obj-C 不够熟悉,无法给出答案,但这里有一个答案 Swift 4. 如有必要,有人可以随意编辑和添加 Obj-C,但我认为这里很清楚如何解决手头的问题,即使它在 Swift.
假设我有一个要弹出的菜单:
let info = the info you need to pass
let currVC = self.getTopMostViewController()
let menuVC = currVC.storyboard?.instantiateViewController(withIdentifier: "myStringIdentifierSetInStoryboard") as? EventMenuViewController
guard menuVC != nil else { return }
menuVC!.info = info // Pass info necessary (i.e. what you would normally pass in prepare(for segue: ...). menuVC.info is a global variable from your class
currVC.present(menuVC!, animated: true, completion: nil)
getTopMostViewController() 的实现可能会有所不同。我的在下面,改编自
func getTopMostViewController() -> UIViewController {
let anyVC = UIViewController()
if var topController = UIApplication.shared.keyWindow?.rootViewController {
while let presentedViewController = topController.presentedViewController {
topController = presentedViewController
}
return topController
}
return anyVC
}
希望对您有所帮助!这不会给您描述 iOS 12 和 Swift 4 的错误,尽管我在尝试使用活动搜索控制器以模态方式呈现时确实得到了那个确切的错误,这就是导致我来到这里的原因。
确保使用
self.searchController = UISearchController()
而不是
self.searchController = UISearchController(searchResultsController: self)