我在我的应用程序中使用ActionSheet . 在我的iPhone上它可以工作,但它不在iPad模拟器上 .
这是我的代码:
@IBAction func dialog(sender: AnyObject) {
let optionMenu = UIAlertController(title: nil, message: "Choose Option", preferredStyle: .ActionSheet)
let deleteAction = UIAlertAction(title: "Delete", style: .Default, handler: {
(alert: UIAlertAction!) -> Void in
println("Filtre Deleted")
})
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: {
(alert: UIAlertAction!) -> Void in
println("Cancelled")
})
optionMenu.addAction(deleteAction)
optionMenu.addAction(cancelAction)
self.presentViewController(optionMenu, animated: true, completion: nil)
}
而我的错误:
由于未捕获的异常'NSGenericException'终止应用程序,原因是:'您的应用程序已呈现样式UIAlertControllerStyleActionSheet的UIAlertController() . 具有此样式的UIAlertController的modalPresentationStyle是UIModalPresentationPopover . 您必须通过警报控制器的popoverPresentationController为此弹出窗口提供位置信息 . 您必须提供sourceView和sourceRect或barButtonItem . 如果在显示警报控制器时不知道此信息,您可以在UIPopoverPresentationControllerDelegate方法-prepareForPopoverPresentation中提供它 .
5 回答
您需要在呈现optionMenu之前提供源视图或按钮,因为在iPad上它是一个UIPopoverPresentationController,正如您在错误中所说的那样 . 这只是意味着您的操作表指向按钮,让用户知道它从哪里开始 .
例如,如果您通过点击右侧导航栏项目来展示您的optionMenu . 你可以这样做:
或者您可以设置这样的视图:(您只需要其中一个2)
另请注意,如果将UIAlertControllerStyle更改为Alert而不是操作表,则无需指定此项 . 我相信你一定想出来了,但我只想帮助遇到这个页面的人 .
Swift 3
如前所述,您应该将UIAlertController配置为在iPAD上的特定点上显示 .
导航栏示例:
对我来说同样的问题 . 我有一个UIAlertController在手机上运行良好,但在iPad上崩溃了 . 从表格视图中点击单元格时,工作表会弹出 .
对于Swift 3,我在呈现之前添加了3行代码:
add statements in the following terms before presented.
optionMenu.popoverPresentationController.sourceView = self.view; optionMenu.popoverPresentationController.sourceRect = CGRectMake(0,0,1.0,1.0);
它会运作良好 .
请注意,如果您尚未将IB中的sourceview链接到应用中的相关变量,则也会出现此错误 .