在 Presented View Controller 的 UITableViewCell 中呈现 UIAlertController

Present UIAlertController in UITableViewCell on Presented View Controller

我想显示 UITableViewCell class 中的 UIAlertController

在搜索时我发现 UIApplication.shared.keyWindow?.rootViewController?.present 这样做会显示它。

如果打开的 ViewControllerNavigationController 下,这会起作用,因为我的 rootViewControllerNavigationController 并且 cell 所在的视图正在展示中。

那么如何在呈现的UITableViewCell中呈现UIAlertController

您需要实施 delegate 模式。

创建一个由您的控制器实现的 protocol 并将此控制器的 weak 引用分配给您的单元格。

然后使用 cell.delegate.didPerformAction()

单元格是一个View,它不应该负责呈现任何东西。

我认为你必须在单元格 Class 中创建一个变量,并且你必须将那些 class 分配给实现单元格的那个变量。

然后在任何操作的单元格中,您只需像下面的代码一样调用警报:

var fromViewController : UIViewController?

@IBAction func accessoryBtnTapped(_ sender: UIButton){

    let alert = UIAlertController(title: "Alert!", message: "It's Alert From Cell", preferredStyle: .alert)
    let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
    alert.addAction(okAction)

    fromViewController?.present(alert, animated: true, completion: nil)
}