ios - UITableViewCell 未显示警报 View

标签 ios uitableview

OrderFormViewController 中,我有一个 tableView ,在 tableViewCell 中,我有一个 cameraButton,点击时会显示警报。

现在,当出现 OrderFormViewController(未推送),它会加载 tableView 及其单元格(行数为 1 硬编码)

我在 cameraButtonIBAction 下有以下代码:

@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/

相关文章:

iphone - 即使在 iphone 中使用异步调用后下载图像也很慢

ios - CarbonKit CarbonTabSwipeNavigation,Viewcontrollers 未正确对齐

ios - 是否可以在 UITableView 的部分行中禁用 didSelectRowAtIndexPath?

ios - TableView 数组映射

android - 特拉维斯和 React Native。适用于 Android 和 ios 的 Parallels 构建

ios - 在 HTTPS 可调用的 Firebase Cloud Functions 中,错误代码的名称是否在 iOS 中可用?

ios - 如何在某些生命周期事件之前/之后初始化 ViewController 的安全属性?

iOS - 除了内存不足之外应用程序崩溃的原因?

iphone - 从数组填充表格仅显示在第一个单元格上

iphone - UItableViewCell 展开/折叠动画(展开时有效,但折叠时无效)