斯威夫特:如何返回到我的UIViewController的相同实例 [英] Swift: How to return to the same instance of my UIViewController

查看:103
本文介绍了斯威夫特:如何返回到我的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆