ActionSheet不工作的iPad

我在我的应用程序中使用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) } 

而我的错误:

终止应用程序,由于未捕获的exception'NSGenericException',原因:'您的应用程序已呈现样式UIAlertControllerStyleActionSheet UIAlertController()。 具有此样式的UIAlertController的modalPresentationStyle是UIModalPresentationPopover。 您必须通过警报控制器的popoverPresentationController为此popup窗口提供位置信息。 您必须提供sourceView和sourceRect或barButtonItem。 如果在显示警报控制器时未知这些信息,则可以在UIPopoverPresentationControllerDelegate方法的-prepareForPopoverPresentation中提供。

你需要提供一个源视图或button之前呈现optionMenu,因为在iPad上它是一个UIPopoverPresentationController,正如它在你的错误中说的。 这只是表示您的操作表指向了让用户知道从何处开始的button。

例如,如果您通过点击右侧的导航栏项目来展示您的optionMenu。 你可以做这样的事情:

 optionMenu.popoverPresentationController?.barButtonItem = self.navigationItem.rightBarButtonItem self.presentViewController(optionMenu, animated: true, completion: nil) 

或者你可以像这样设置一个视图:(你只需要其中的一个)

 optionMenu.popoverPresentationController?.sourceView = yourView self.presentViewController(optionMenu, animated: true, completion: nil) 

另外请记住,如果您将UIAlertControllerStyle更改为Alert,而不是操作表,则不需要指定。 我相信你肯定已经知道了,但我只是想帮助任何遇到此页面的人。

Swift 3

如前所述,您应该将UIAlertControllerconfiguration为在iPAD上的特定点上显示。

导航栏示例:

  // 1 let optionMenu = UIAlertController(title: nil, message: "Choose an option", preferredStyle: .actionSheet) // 2 let deleteAction = UIAlertAction(title: "Option 1", style: .default, handler: { (alert: UIAlertAction!) -> Void in print("option 1 pressed") }) let saveAction = UIAlertAction(title: "Option 2", style: .default, handler: { (alert: UIAlertAction!) -> Void in print("option 2 pressed") }) // let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: { (alert: UIAlertAction!) -> Void in print("Cancelled") }) // 4 optionMenu.addAction(deleteAction) optionMenu.addAction(saveAction) optionMenu.addAction(cancelAction) // 5 optionMenu.popoverPresentationController?.barButtonItem = self.navigationItem.rightBarButtonItem self.present(optionMenu, animated: true) { print("option menu presented") } 

同样的问题对我来说。 我有一个UIAlertController在手机上工作正常,但在iPad上坠毁。 从表格视图中轻敲单元格时,将popup该表格。

对于Swift 3,我在呈现之前添加了3行代码:

  ... sheet.popoverPresentationController?.sourceView = self.view sheet.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection() sheet.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0) self.present(sheet, animated: true, completion: nil) 

在呈现之前添加以下术语的陈述。

optionMenu.popoverPresentationController.sourceView = self.view; optionMenu.popoverPresentationController.sourceRect = CGRectMake(0,0,1.0,1.0);

 @IBAction func dialog(sender: AnyObject) { ... optionMenu.popoverPresentationController.sourceView = self.view; optionMenu.popoverPresentationController.sourceRect = CGRectMake(0,0,1.0,1.0); self.presentViewController(optionMenu, animated: true, completion: nil) } 

它会运作良好。

只要注意,如果您还没有将IB中的sourceview链接到应用程序中的相关variables,也可以得到这个错误。