ios ActionSheet 不工作 iPad
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28089898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ActionSheet not working iPad
提问by Stephany
I'm using ActionSheet in my application. On my iPhone it works, but it doesn't on the iPad simulator.
我在我的应用程序中使用 ActionSheet。在我的 iPhone 上它可以工作,但在 iPad 模拟器上却没有。
this is my code:
这是我的代码:
@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)
}
And my error:
而我的错误:
Terminating app due to uncaught exception 'NSGenericException', reason: 'Your application has presented a UIAlertController () of style UIAlertControllerStyleActionSheet. The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover. You must provide location information for this popover through the alert controller's popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem. If this information is not known when you present the alert controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.'
由于未捕获的异常“NSGenericException”而终止应用程序,原因:“您的应用程序已呈现 UIAlertControllerStyleActionSheet 样式的 UIAlertController ()。具有此样式的 UIAlertController 的 modalPresentationStyle 是 UIModalPresentationPopover。您必须通过警报控制器的 popoverPresentationController 提供此弹出框的位置信息。您必须提供 sourceView 和 sourceRect 或 barButtonItem。如果在您呈现警报控制器时不知道此信息,您可以在 UIPopoverPresentationControllerDelegate 方法 -prepareForPopoverPresentation 中提供它。
回答by MD Singh
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.
您需要在显示 optionMenu 之前提供源视图或按钮,因为在 iPad 上它是一个 UIPopoverPresentationController,正如它在您的错误中所说。这只是意味着您的操作表指向按钮,让用户知道它从哪里开始。
For example if you're presenting your optionMenu by tapping on the right navigation bar item. You could do something like this:
例如,如果您通过点击右侧导航栏项目来显示您的 optionMenu。你可以这样做:
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)
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.
另请记住,如果您将 UIAlertControllerStyle 更改为 Alert 而不是操作表,则无需指定此项。我相信您一定已经想通了,但我只是想帮助遇到此页面的任何人。
回答by Zach
Same problem for me. I had a UIAlertController that worked fine on phone, but crashed on iPad. The sheet pops up when a cell is tapped from a table view.
对我来说同样的问题。我有一个 UIAlertController 在手机上运行良好,但在 iPad 上崩溃了。当从表格视图中点击单元格时,表格会弹出。
For Swift 3, I added 3 lines of code right before presenting it:
对于 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)
回答by mourodrigo
Swift 3
斯威夫特 3
As said before, you should configure UIAlertController to be presented on a specific point on iPAD.
如前所述,您应该将 UIAlertController 配置为在 iPAD 上的特定点上显示。
Example for navigation bar:
导航栏示例:
// 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")
}
回答by Mohit Singh
If you wish to present it in the centre with no arrows [Swift 3+]:
如果您希望在没有箭头的情况下将其显示在中心 [ Swift 3+]:
if let popoverController = optionMenu.popoverPresentationController {
popoverController.sourceView = self.view
popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
popoverController.permittedArrowDirections = []
}
self.present(optionMenu, animated: true, completion: nil)
回答by Jimmy chou
add statements in the following terms before presented.
在介绍之前添加以下条款中的陈述。
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)
}
it will work well.
它会运作良好。
回答by Peter Johnson
Just a note that you can also get this error if you haven't linked the sourceview in IB to the relevant variable in your app.
请注意,如果您尚未将 IB 中的 sourceview 链接到应用程序中的相关变量,您也可能会收到此错误。
回答by Asad Farooq
you need to add this for Ipad
你需要为 Ipad 添加这个
alertControler.popoverPresentationController?.sourceView = self.view
alertControler.popoverPresentationController?.sourceView = self.view
回答by Andrea Miotto
I've done this package to manage ActionSheet and Popover in iPhone, Ipad and Mac. https://github.com/AndreaMiotto/ActionOver
我已经完成了这个包来管理 iPhone、Ipad 和 Mac 中的 ActionSheet 和 Popover。 https://github.com/AndreaMiotto/ActionOver