在 OrderFormViewController
中,我有一个 tableView
,在 tableViewCell 中,我有一个 cameraButton
,点击时会显示警报。
现在,当出现 OrderFormViewController
时(未推送),它会加载 tableView
及其单元格(行数为 1 硬编码) 。
我在 cameraButton
的 IBAction
下有以下代码:
@IBAction func cameraButtonPressed(_ sender: Any) {
//DispatchQueue.main.async {
let alert = UIAlertController(title: "Choose Image From", message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: { _ in
if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.camera))
{
self.imagePicker.sourceType = UIImagePickerControllerSourceType.camera
self.imagePicker.allowsEditing = true
UIApplication.shared.keyWindow?.rootViewController?.present(self.imagePicker, animated: true, completion: nil)
}
else
{
let alert = UIAlertController(title: "Warning", message: "You don't have camera", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
UIApplication.shared.keyWindow?.rootViewController?.present(alert, animated: true, completion: nil)
}
}))
alert.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { _ in
self.imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
self.imagePicker.allowsEditing = false
UIApplication.shared.keyWindow?.rootViewController?.present(self.imagePicker, animated: true, completion: nil)
}))
//alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
alert.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil))
UIApplication.shared.keyWindow?.rootViewController?.present(alert, animated: true, completion: nil)
//}
}
我已经搜索过这个,所以这就是为什么我尝试 UIApplication.shared.keyWindow?.rootViewController
并尝试从 DispatchQueue.main.async
运行它,但是一些UIApplication.shared.keyWindow?.rootViewController?.present(alert,animated:true,completion:nil)
不起作用。
更新 如果我按下 View Controller ,它就会工作。但我需要一个针对呈现 View Controller 的解决方案。
最佳答案
我强烈建议重构整个解决方案以使用委托(delegate) - 将显示警报的操作从 UITableViewCell
委托(delegate)给 OrderFormViewController
,然后只需使用
self.present(alert, animated: true, completion: nil)
因为使用 UIApplication.shared.keyWindow?.rootViewController
非常脆弱。
所以只是为您勾画一下,我相信最好的方法是:
class OrderFormViewController: UITableViewController {
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! CustomCell
// set the delegate
cell.delegate = self
return cell
}
}
extension OrderFormViewController: CustomCellDelegate {
func customCellDelegateButtonPressed(_ customCell: CustomCell) {
let alert = UIAlertController(title: "Choose Image From", message: nil, preferredStyle: .alert)
// configure the alert
// now the presentation of the alert is delegated here to OrderFormViewController, so it can simply use self to present
self.present(alert, animated: true, completion: nil)
}
}
protocol CustomCellDelegate: class {
func customCellDelegateButtonPressed(_ customCell: CustomCell /*, potentially more parameters if needed */)
}
class CustomCell: UITableViewCell {
// rest omitted for brevity
weak var delegate: CustomCellDelegate?
@IBAction func cameraButtonPressed(_ sender: Any) {
delegate?.customCellDelegateButtonPressed(self)
}
}
关于ios - UITableViewCell 未显示警报 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48316660/