【问题标题】:ActionSheet not working iPadActionSheet 不工作 iPad
【发布时间】:2015-03-21 07:28:08
【问题描述】:

我在我的应用程序中使用 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。你 必须通过警报提供此弹出窗口的位置信息 控制器的 popoverPresentationController。您必须提供 sourceView 和 sourceRect 或 barButtonItem。如果这个信息是 当您出示警报控制器时不知道,您可以在 UIPopoverPresentationControllerDelegate 方法 -prepareForPopoverPresentation。'

【问题讨论】:

  • 这个Link 可以帮助你。
  • ios 8 及更高版本没有操作表 UIActionController 实例,您需要将类型设置为 UIAlertControllerStyleActionSheet .... 这可能会对您有所帮助 .... 虽然 iPad 建议使用 uipopover ....
  • 您必须在 iPad 上将其呈现为弹出框

标签: ios ipad swift uiactionsheet


【解决方案1】:

你需要为 Ipad 添加这个

alertControler.popoverPresentationController?.sourceView = self.view

popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)

【讨论】:

    【解决方案2】:

    我已经完成了这个包来管理 iPhone、Ipad 和 Mac 中的 ActionSheet 和 Popover。 https://github.com/AndreaMiotto/ActionOver

    【讨论】:

      【解决方案3】:

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

      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】:

      如果你想在中间不带箭头显示[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】:

        请注意,如果您没有将 IB 中的源视图链接到应用程序中的相关变量,也可能会出现此错误。

        【讨论】:

          【解决方案6】:

          对我来说同样的问题。我有一个 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)
          

          【讨论】:

          • 这在 Swift 5.0 中对我有用,但我不知道如何从视图底部显示弹出窗口。谢谢!
          • @FlorentinLupascu: 只需将 allowedArrowDirections 设置为 UIPopoverArrowDirection.Down 和 sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.bottom, width: 0, height: 0 )
          【解决方案7】:

          斯威夫特 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")
              }
          

          【讨论】:

            【解决方案8】:

            您需要在显示 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 而不是操作表,则无需指定此项。 我相信你一定已经明白了,但我只是想帮助任何看到这个页面的人。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2016-12-15
              • 2021-11-05
              • 2019-11-16
              • 2011-10-31
              • 2019-01-20
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多