首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ActionSheet不工作iPad

ActionSheet不工作iPad
EN

Stack Overflow用户
提问于 2015-01-22 21:24:30
回答 8查看 29.7K关注 0票数 86

我在我的应用程序中使用ActionSheet。在我的iPhone上它可以工作,但在iPad模拟器上不行。

这是我的代码:

代码语言:javascript
复制
@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方法NSGenericException中提供它

EN

回答 8

Stack Overflow用户

发布于 2015-03-18 12:25:52

你需要在展示optionMenu之前提供一个源视图或按钮,因为在iPad上它是一个UIPopoverPresentationController,正如它在你的错误中所说的那样。这只是意味着您的操作表单指向按钮,让用户知道它是从哪里开始的。

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

代码语言:javascript
复制
optionMenu.popoverPresentationController?.barButtonItem = self.navigationItem.rightBarButtonItem

self.presentViewController(optionMenu, animated: true, completion: nil)

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

代码语言:javascript
复制
optionMenu.popoverPresentationController?.sourceView = yourView

self.presentViewController(optionMenu, animated: true, completion: nil)

还请记住,如果您将UIAlertControllerStyle更改为Alert而不是action sheet,则不需要指定此项。我相信你一定已经弄明白了,但我只是想帮助任何遇到这个页面的人。

票数 110
EN

Stack Overflow用户

发布于 2017-06-01 15:56:46

我也有同样的问题。我有一台在手机上运行良好的UIAlertController,但在iPad上崩溃了。当从表视图中点击单元格时,工作表将弹出。

对于Swift 3,我在展示它之前添加了3行代码:

代码语言:javascript
复制
        ...

        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)
票数 33
EN

Stack Overflow用户

发布于 2016-11-11 20:11:54

Swift 3

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

导航栏示例:

代码语言:javascript
复制
    // 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")
    }
票数 24
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28089898

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档