无法使用警报操作定向到另一个视图控制器

Unable to direct to another view controller using alert action

我希望在出现的警报操作中单击“确定”按钮时将用户定向到另一个视图控制器。单击“确定”按钮时必须指导用户,而不是在显示此警报时立即指导用户。这是我尝试过的:

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

该应用程序没有崩溃,但在单击“确定”按钮时它也没有指向我的其他视图控制器。我该如何解决这个问题?

如果有人能帮助我,我将非常高兴!

编辑:我相信我的问题被误解了,所以我试着把事情解释得更清楚,抱歉我的英语不好。

转到故事板中所谓的 "FourthViewController",并将标识符设置为那个 - 这与名称不同,因此可能会造成混淆。我已附上屏幕截图,希望对您有所帮助!

并且您希望警报执行的操作需要由 "handler" 处理,通常我更喜欢将其放在它自己的方法中,但为简单起见,以下内容适用于您的情况:

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: "FourthViewController")
    self.navigationController!.pushViewController(uivc, animated: true)
}))
self.present(alert, animated: true)
  1. Make sure you are calling correct nib file
  2. Make sure you set storyboard id in storyboard .