Swift警报动作处理程序不工作特定情况

问题描述 投票:0回答:2

我正在创建一个显示用户评论的应用。用户输入注释,然后单击“提交”按钮,然后应发生警报操作视图。当点击警报视图中的确定按钮时,我正试图将用户定向到我的第四视图控制器。这是我的代码,应该可以正常工作。

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是根据用户在过去三个视图控制器上的选择创建的。有一个独特的第四视图控制器,它基于在先前视图控制器中单击的tableview单元的组合。我相信问题出现是因为当我将用户引导到FourthViewController时,应用程序不知道FourthViewController包含什么,因为之前没有点击过tableview单元格。当我将VC的方向从FourthViewController更改为FirstViewController时,一切都运行得非常好。

有可能解决这个问题吗?我很感激任何帮助!非常感谢你,祝你有个美好的一天!

ios swift alert handler
2个回答
0
投票

如果我理解正确,FourthViewController有一些依赖于以前的ViewControllers的变量。在这种情况下,只需初始化这些变量设置它们的值:uivc.varName = value

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


0
投票

删除以下行表单操作完成

  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)
© www.soinside.com 2019 - 2024. All rights reserved.