ActionSheet无法使用iPad

时间:2015-01-22 13:24:30

标签: ios ipad swift uiactionsheet

我在我的应用程序中使用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'而终止应用,   原因:'您的应用程序提供了一个UIAlertController   ()风格   UIAlertControllerStyleActionSheet。一个modalPresentationStyle   具有此样式的UIAlertController是UIModalPresentationPopover。您   必须通过警报提供此弹出窗口的位置信息   controller的popoverPresentationController。你必须提供一个   sourceView和sourceRect或barButtonItem。如果这个信息是   当您出示警报控制器时不知道,您可以提供它   UIPopoverPresentationControllerDelegate方法   -prepareForPopoverPresentation'。

8 个答案:

答案 0 :(得分:101)

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

例如,如果您通过点击右侧导航栏项目来展示您的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而不是操作表,则无需指定此项。 我相信你一定已经明白了,但我只是想帮助那些遇到这个页面的人。

答案 1 :(得分:24)

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

对于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)

答案 2 :(得分:22)

Swift 3

如前所述,您应该将UIAlertController配置为在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")
    }

答案 3 :(得分:4)

在呈现之前使用以下术语添加语句。

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)
}

它会很好用。

答案 4 :(得分: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)

答案 5 :(得分:0)

请注意,如果您未将IB中的源视图与应用中的相关变量相关联,也会出现此错误。

答案 6 :(得分:0)

您需要为Ipad添加它

alertControler.popoverPresentationController?.sourceView = self.view

答案 7 :(得分:0)

我已经完成了此程序包的管理,以便在iPhone,Ipad和Mac中管理ActionSheet和Popover。 https://github.com/AndreaMiotto/ActionOver