segue创建视图控制器swift的新实例
问题描述:
我有一个应用程序与四个视图控制器。 1,2和3之间的导航是好的,但从视图控制器1你可以选择2或4(这是我的设置)我有一个从1到4的segue。然后我使用unwind segue来取回。但是当我使用segue回到4时,它实例化了一个新的实例4.segue创建视图控制器swift的新实例
我的问题:有没有什么办法可以访问用户上次看到的视图控制器的同一个实例。
答
就像什么@ 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
}
}
+0
感谢所有帮助 – c3pNoah
+0
@ c3pNoah标记我是正确的答案,并且upvote是否有帮助谢谢 –
你可以保持到视图控制器的引用,并推动它,而不是USI另一个例子ng一个segue,无零你使用的导航控制器,你通常会推一个新的实例,根据需要配置它 – Paulw11
对不起,我是一种新手,不知道你的意思。能否请你详细解释一下 – c3pNoah