gpt4 book ai didi

iOS:无法将 NextViewContrller 类型的值转换为 UINavigationController

转载 作者:行者123 更新时间:2023-11-29 00:16:27 25 4
gpt4 key购买 nike

我正在尝试实现 navegationController 以编程方式在 View Controller 之间切换:

enter image description here

@IBAction func next(_ sender: Any) {
self.performSegue(withIdentifier: "next", sender: self)

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "next"{
let destNVC = segue.destination as! UINavigationController
let next = destNVC.topViewController as! NextViewController
next.title = "next!"
}
}

但是我收到了这个错误:

Could not cast value of type 'myProject.NextViewController' (0x108de5338) to 'UINavigationController' (0x10aa2d4a8).

enter image description here

你们中有人知道我为什么会收到此错误吗?

非常感谢您的帮助。

最佳答案

我假设屏幕截图中显示的最右边的 VC 是您的 NextViewController

让我们看看这种情况下的 segue.destination 是什么。很容易找到 segue.destination 所指的内容。查看你的 Storyboard,找到你想要的 segue,然后用箭头找到 segue 的结尾。无论箭头指向什么 VC,都是segue.destination。 (segue 的另一端是 segue.source 顺便说一句)

既然我们知道了这一点,我们就知道在这种情况下 segue.destination 必须引用 NextViewController。现在您应该意识到应该更正此行:

let destNVC = segue.destination as! UINavigationController

好吧,尝试将 NextViewController 转换为 UINavigationController 肯定会导致错误,不是吗?

事实上,您可以将 segue.destination 转换为 NextViewController!

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "next"{
let next = segue.destination as! NextViewController
next.title = "next!"
}
}

关于iOS:无法将 NextViewContrller 类型的值转换为 UINavigationController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45202391/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com