UIAlertController 崩溃错误
UIAlertController Crash Error
这是我的代码
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 就出现在导航控制器的正上方,非常小,有人知道解决方法吗?
在 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 中的单元格进行选择后显示操作 sheet。您可以使用此代码:
alertController.popoverPresentationController.sourceView = cell
alertController.popoverPresentationController.sourceRect = cell.bounds
这是我的代码
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 就出现在导航控制器的正上方,非常小,有人知道解决方法吗?
在 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 中的单元格进行选择后显示操作 sheet。您可以使用此代码:
alertController.popoverPresentationController.sourceView = cell
alertController.popoverPresentationController.sourceRect = cell.bounds