gpt4 book ai didi

ios - 使用 instantiateViewControllerWithIdentifier 访问相同的 View Controller

转载 作者:行者123 更新时间:2023-11-28 11:07:43 25 4
gpt4 key购买 nike

我在 Storyboard 中有一个 UIViewController,其 Storyboard ID 称为“MyViewController”(嵌入在 UINavigationController 中作为入口点),我试图使用 instantiateViewControllerWithIdentifier 从另一个类访问它,例如:

MyAccessor.swift

func accessMyViewController(){
let storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let vc = storyboard.instantiateViewControllerWithIdentifier("MyViewController")
print(vc.description)
}

MyViewController.swift

override func viewDidLoad() {
print(self.description)
}

控制台

<MyViewController: 0x7ff6ab76f9c0>
<MyViewController: 0x7ff6ab557410>

我打印出了不同的内存地址,

他们不应该是同一个人吗?

最佳答案

可能您的 navigationController 是 Storyboard入口点

因此,如果这是真的,则有两个实例..一个在您的应用程序启动期间创建,另一个从您的代码中以编程方式调用。

P.S.:viewDidLoad 不会仅通过实例化 View Controller 来调用,参见 documentation有关详细信息..,否则您已经看到了三个打印品..

enter image description here

如果您想立即查看您的情况,请尝试修改您的打印行:

MyAccessor.swift

func accessMyViewController(){
...
print("∙ \(NSStringFromClass(self.dynamicType)) - vc details : \(vc.description) ")
}

MyViewController.swift

override func viewDidLoad() {
...
print("∙ \(NSStringFromClass(self.dynamicType)) - self details : \(self.description) ")
}

关于ios - 使用 instantiateViewControllerWithIdentifier 访问相同的 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36738851/

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