【问题标题】:UIAlertController Crash Error [duplicate]UIAlertController 崩溃错误 [重复]
【发布时间】:2018-07-14 10:22:39
【问题描述】:

这是我的代码

let selectRoute = UIAlertController(title: "Select a Route", message: "Please select the route you want to create your trip on", preferredStyle: .actionSheet)

    let route1 = UIAlertAction(title: "Route 1", style: .default) { (action) in
        self.reminder()
        self.routeID = 1
        self.tableView.reloadData()
    }

    let route2 = UIAlertAction(title: "Route 2", style: .default) { (action) in

    }

    selectRoute.addAction(route1)
    selectRoute.addAction(route2)

    if let popoverPresentationController = selectRoute.popoverPresentationController {
        popoverPresentationController.sourceView = self.view
        popoverPresentationController.sourceRect = self.view.bounds
    }

    self.present(selectRoute, animated: true, completion: nil)

当我运行这个时,actionSheet 只是出现在导航控制器上,非常小,有人知道解决方法吗?

【问题讨论】:

  • 你如何展示UIAlertController
  • 让我猜猜:iPad 崩溃了,不是吗?你找到错误了吗?
  • @Larme 是线程 1 信号 sigabrt 错误
  • 您是否尝试显示警报或操作表,因为UIAlertController 可以同时执行这两种操作?警报不会生成错误,但操作表会生成错误,因为它需要锚定到错误指示的内容。
  • @UpholderOfTruth 行动表

标签: swift swift4 uialertcontroller


【解决方案1】:

在 iPad 上,警报将使用 UIPopoverPresentationController 显示为弹出框,它需要您使用 sourceView 和 sourceRect 或 barButtonItem 为弹出框的呈现定义一个锚点。

要支持 iPad,请包含以下代码:

alertController.popoverPresentationController?.sourceView = self.view
alertController.popoverPresentationController?.sourceRect = self.myButton.frame
self.presentViewController(alertController, animated: true, completion: nil)

这是一个例子: 假设您有一个按钮,并且您将在按钮下方显示警报控制器:

@IBOutlet weak var myBtn: UIButton!

let alertController = UIAlertController(title: "title :)", message:"message...", preferredStyle: .actionSheet)
let defaultAction = UIAlertAction(title: "cancel", style: .cancel, handler: nil)

let deleteAction = UIAlertAction(title: "delete", style: .destructive) { (action: UIAlertAction) in
        //Do something
    }

let addAction = UIAlertAction(title: "...", style: .default) { (action) in
        //do something
    }

alertController.addAction(addAction)
alertController.addAction(deleteAction)
alertController.addAction(defaultAction)

alertController.popoverPresentationController?.sourceRect = self.myBtn.frame
alertController.popoverPresentationController?.sourceView = self.view
self.present(alertController, animated: true, completion: nil)

如果您在用户对 UITableView 中的单元格进行选择后显示操作表。您可以使用此代码:

alertController.popoverPresentationController.sourceView = cell
alertController.popoverPresentationController.sourceRect = cell.bounds

【讨论】:

  • No 不起作用,这在 iPhone 上还能用吗?
  • @BobHenderson 是的,它适用于 iPhone 和 iPad。
  • “细胞”应该是什么?
  • @BobHenderson 在我的示例中,我想在我的 tableview 单元格上显示警报。忘记它,只需设置一个 sourceRect 和一个 sourceView。像这样:alertController.popoverPresentationController?.sourceView = self.view alertController.popoverPresentationController?.sourceRect = myRect
  • myRect 是什么意思?
猜你喜欢
  • 2015-10-13
  • 2013-10-10
  • 2017-03-24
  • 2015-01-09
  • 2017-02-20
  • 2011-02-25
  • 2017-05-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多