斯威夫特:如何返回到我的UIViewController的相同实例 [英] Swift: How to return to the same instance of my UIViewController
问题描述
我的SettingsViewController
中有一个按钮,当按下该按钮时,我想在每次按下该按钮时显示相同的TimerViewController
实例.
I have a button in my SettingsViewController
that when pressed, I want to present the same instance of my TimerViewController
each time the button is pressed.
I think I have gotten fairly close with this post here, iOS Swift, returning to the same instance of a view controller. So if you could point me to saving the instance of TimerViewController
, that would work the best.
这是我现在拥有的代码-
This is the code I have right now -
var yourVariable : UIViewController!
if yourVariable == nil {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
yourVariable = storyboard.instantiateViewControllerWithIdentifier("timer") as! TimerInterface
}
presentViewController(yourVariable, animated: true, completion: nil)
推荐答案
您提供的代码应该可以使用.如果SettingsViewController
被解除分配,但timerViewController
也被解除分配并在下次显示时重新创建.因此您必须确保将其实例保存在适当的位置.
the code you provided should work. if your SettingsViewController
gets deallocated though the timerViewController
also gets deallocated and recreated the next time you present it. so you have to make sure to save its instance at an appropriate location.
var timerViewController: TimerViewController!
if timerViewController == nil {
let timerViewController = UIStoryboard(name: "Main", bundle: nil)
yourVariable = storyboard.instantiateViewControllerWithIdentifier("timer") as! TimerInterface
}
presentViewController(timerViewController, animated: true, completion: nil)
这篇关于斯威夫特:如何返回到我的UIViewController的相同实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!