Ios TableViewCell未显示alertView
在Ios TableViewCell未显示alertView,ios,uitableview,Ios,Uitableview,在OrderFormViewController中,我有一个tableView和tableViewCell,我有一个camerarbutton,点击该按钮时会显示警报 现在,当显示(OrderFormViewController时,它加载tableView及其单元格(行计数为1硬编码) 我在cameraButton的iAction下有以下代码: @IBAction func cameraButtonPressed(_ sender: Any) { //DispatchQueue.
OrderFormViewController
中,我有一个tableView
和tableViewCell,我有一个camerarbutton
,点击该按钮时会显示警报
现在,当显示(OrderFormViewController
时,它加载tableView
及其单元格(行计数为1硬编码)
我在cameraButton
的iAction
下有以下代码:
@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(警报,动画:true,完成:nil)
不起作用
更新如果我按下视图控制器,它就会工作。但是我需要一个用于显示视图控制器的解决方案。我强烈建议重构整个解决方案以使用委托-将显示警报的操作从
UITableViewCell
委托给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)
}
}
为什么不使用
self.present(警报,动画:true,完成:nil)
?使用委托模式将演示任务委托给OrderFormViewController
,无论OrderFormViewController
如何进入屏幕,您都可以在那里演示。。更多信息请参见我的答案details@Venkat因为它是tableViewCell,没有自引用。