问题描述
我在我的应用程序中使用 ActionSheet.在我的 iPhone 上它可以工作,但在 iPad 模拟器上却没有.
I'm using ActionSheet in my application. On my iPhone it works, but it doesn't on the iPad simulator.
这是我的代码:
@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"而终止应用程序,原因:'您的应用程序提供了一个 UIAlertController() 的风格UIAlertControllerStyleActionSheet.的 modalPresentationStyle这种风格的 UIAlertController 是 UIModalPresentationPopover.你必须通过警报提供此弹出窗口的位置信息控制器的 popoverPresentationController.您必须提供一个sourceView 和 sourceRect 或 barButtonItem.如果这些信息是不知道何时呈现警报控制器,您可以在UIPopoverPresentationControllerDelegate 方法-prepareForPopoverPresentation.'
推荐答案
您需要在显示 optionMenu 之前提供源视图或按钮,因为在 iPad 上它是一个 UIPopoverPresentationController,正如它在您的错误中所说的那样.这只是意味着您的操作表指向按钮,让用户知道它从哪里开始.
You need to provide a source view or button just before presenting optionMenu since on iPad its a UIPopoverPresentationController, As it says in your error. This just means that your action sheet points to the button letting the user know where it started from.
例如,如果您通过点击右侧导航栏项目来显示您的选项菜单.你可以这样做:
For example if you're presenting your optionMenu by tapping on the right navigation bar item. You could do something like this:
optionMenu.popoverPresentationController?.barButtonItem = self.navigationItem.rightBarButtonItem
self.presentViewController(optionMenu, animated: true, completion: nil)
或者你可以设置一个这样的视图:(你只需要这两个中的一个)
or you could set a view like this:(You just need one of these 2)
optionMenu.popoverPresentationController?.sourceView = yourView
self.presentViewController(optionMenu, animated: true, completion: nil)
另外请记住,如果您将 UIAlertControllerStyle 更改为 Alert 而不是操作表,则无需指定此项.我相信您一定已经想通了,但我只是想帮助遇到此页面的任何人.
Also keep in mind that if you change your UIAlertControllerStyle to Alert instead of action sheet, You wouldn't need to specify this.I am sure you must have figured it out but i just wanted to help anyone who comes across this page.
这篇关于ActionSheet 不工作 iPad的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!