在 UIPopoverPresentationController 中崩溃的应用,但没有明确的弹出窗口?
问题描述:在UIPopoverPresentationController中发生应用程序崩溃,但没有明确的弹出窗口。
问题原因:最可能是在iPad上发生的ActionSheet
崩溃。
解决方法:您应该添加一个if条件,如下所示:
if let popoverController = alertVC.popoverPresentationController {
popoverController.sourceView = self.view
popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
popoverController.permittedArrowDirections = []
}
该问题的出现原因是在iOS 9+版本中,在UIPopoverPresentationController presentationTransitionWillBegin
方法中发生了崩溃,而崩溃的原因是sourceView
属性为nil
。
解决方法是确保sourceView
属性不为nil
,可以通过以下方式实现:
UIViewController *vc = <#instance#>. vc.modalPresentationStyle = UIModalPresentationPopover; vc.popoverPresentationController.delegate = self; vc.popoverPresentationController.sourceView = sourceView; // <--- 这里一定不能为nil. vc.popoverPresentationController.sourceRect = sourceView.bounds; [self presentViewController:vc animated:YES completion:nil];
这个问题的原因是在使用UIAlertController时,如果使用ActionSheet样式,iPhone上可以正常显示,但在iPad上需要设置sourceView,否则会导致应用崩溃。
解决方法是在iPad上设置sourceView,并且可以在iPhone上也设置,不会有任何问题。尽管问题的位置没有明确的提示,但可以通过检查代码来找到问题所在。