gpt4 book ai didi

ios - segue 快速创建 View Controller 的新实例

转载 作者:可可西里 更新时间:2023-11-01 01:21:55 25 4
gpt4 key购买 nike

我有一个带有四个 View Controller 的应用程序。 1、2 和 3 之间的导航很好,但是从 View Controller 1 您可以选择转到 2 或 4(这是我的设置)我有一个从 1 到 4 的转场。然后我使用展开转场返回.但是当我使用 segue 回到 4 时,它会实例化一个新的 4 实例。

我的问题:有什么方法可以访问用户上次拥有的同一 View Controller 实例。

最佳答案

就像@Paul11 在评论中所说的那样,如果您希望访问相同的实例,您应该保留您尝试推送的UIViewController引用

比如说

var someViewController = SomeViewController() // << this is at the class level scope

func someSampleFunc() {
// doing this would create a new instance of the `SomeViewController` every time you push
self.navigationController?.pushViewController(SomeViewController(), animated: true)

// whereas if you use the variable which is at the class level scope the memory instance is kept
self.navigationController?.pushViewController(someViewController, animated: true)
}

实例的另一个例子

class Bro {
var name = "Some Name"

func sayDude() {
// since `name` is a class level you can access him here

let dude = "DUUUUUUUDE" // this variable's lifetime only exists inside the `sayDude` function, therefore everytime you call `sayDude()` a new instance of `dude` is created

print(name)
print(dude)
}

func doBackflip() {
sayDude() //

print(name + " does backflip") // since `name` is a class level you can access him here
}
}

关于ios - segue 快速创建 View Controller 的新实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43443170/

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