Swift 警报操作处理程序在特定情况下不起作用

Swift alert action handler not working specific case

我正在创建一个显示用户评论的应用程序。用户输入评论,然后单击提交按钮,然后应出现警报操作视图。当单击警报视图中的确定按钮时,我试图将用户定向到我的 fourthviewcontroller。这是我的代码,它应该可以正常工作。

let alert = UIAlertController(title: "Succesful", message: "Successfully added!", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { _ in
let uivc = self.storyboard!.instantiateViewController(withIdentifier: "ViewController")
    self.navigationController!.pushViewController(uivc, animated: true)
 }))
 self.present(alert, animated: true)

但是,当我单击“确定”按钮时出现此错误。

'InvalidPathValidation', 原因:'(child:) 必须是非空字符串且不包含'.' '#' '$' '[' 或 ']''

FourthViewController 是根据用户对过去三个viewcontroller 的选择创建的。有一个独特的 fourthviewcontroller 基于在以前的视图控制器中单击的 tableview 单元格的组合。我相信问题的出现是因为当我将用户定向到 FourthViewController 时,应用程序不知道 FourthViewController 包含什么,因为之前没有单击过 tableview 单元格。当我将 VC 的方向从 FourthViewController 更改为 FirstViewController 时,一切都运行得非常好。

这个问题可以解决吗?我将不胜感激任何帮助!非常感谢,祝您有愉快的一天!

如果我没理解错的话,FourthViewController 有一些变量依赖于之前的 ViewController。在那种情况下,只需初始化这些变量并设置它们的值:uivc.varName = value

您也可以执行 segue 而不是实例化视图控制器,并且可以在准备函数中设置 nextViewController 的值。

删除以下行表格动作完成

  let uivc = self.storyboard!.instantiateViewController(withIdentifier:"ViewController")
  self.navigationController!.pushViewController(uivc, animated: true)

并创建如下函数

 func navigate(){

 let uivc = self.storyboard!.instantiateViewController(withIdentifier:"ViewController")
  self.navigationController!.pushViewController(uivc, animated: true)

   }

现在在动作完成中添加以下内容

 self.navigate() 

喜欢下面

let alert = UIAlertController(title: "Succesful", message: "Successfully added!", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { _ in
 self.navigate() 
  }))
 self.present(alert, animated: true)