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

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

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

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)

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

如果有人可以帮助我,我会很高兴的!

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

ios swift cocoa-touch storyboard alert
2个回答
1
投票

转到故事板中所谓的“FourthViewController”,并将标识符设置为 - 这与名称不同,因此可能会令人困惑。我附上了截图,希望它有所帮助!

enter image description here

并且你想要通过警报执行的操作需要由“处理程序”处理,通常我更喜欢将它放在它自己的方法中,但为了简单起见,以下内容适用于您的情况:

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)

0
投票
  1. 确保您正在调用正确的nib文件
  2. 确保在storyboard中设置storyboard id。
© www.soinside.com 2019 - 2024. All rights reserved.