我有一个带有四个视图控制器的应用程序。 1,2和3之间的导航很好但是从视图控制器1你可以选择转到2或4(这是我的设置)我有一个从1到4的segue。然后我使用展开segue来取回。但是当我使用segue回到4时,它实例化了一个新的4实例。
我的问题:有没有办法访问用户上次使用的视图控制器的同一个实例。
答案 0 :(得分:2)
就像@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
}
}