ios - 我如何在替换旧的 Storyboard上使用相同的 UIViewController

标签 ios swift uiviewcontroller storyboard

我正在编写一个游戏。这是一个文字游戏。 游戏页面始终相同,内容不同。 当我使用 instantiateViewControllerWithIdentifier("SB") 时,新页面被添加到队列中。但是这样使用的内存就会变高。

如何用新页面替换旧页面?

最佳答案

您可以保留对该类的引用,并在其中包含一个方法来加载其中的内容。

class gameViewController : UIViewController {

   func loadContent() {
      //  this method should refresh content everytime.
    }

}

在其他一些类中,只要您愿意,只需创建对上一类的引用即可。

class SomeClass { 
  var referenceToGameClass : gameViewController!


   func someFunc() {
      self.referenceToGameClass.loadContent()
   }
}

但是如何在离开 gameViewController 类时保存相同的引用呢?您可以在您的 prepareforsegue 方法中添加这一行,假设在转到上面提到的“SomeClass”时:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
      if(segue.identifier == "segueID") {
          let svc = segue.destinationViewController as! SomeClass
        svc.referenceToGameClass = self
    }

}

为了方便,要从“SomeClass”移回同一个 gameViewController,只需使用:

self.presentViewController(self.referenceToGameClass, animated: true, completion: nil)

这可能有点令人困惑,但如果您仔细阅读几遍,就会明白。你也可以问我问题!

关于ios - 我如何在替换旧的 Storyboard上使用相同的 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37906874/

相关文章:

json - 解码对象,其中一个对象是JSON字符串

iphone - UiTextField 和 resignFirstResponder

iphone - 如何使用sendsynchronousRequest :queue:completionHandler:

ios - UIWebView 不呈现位置为 :relative or absolute until scroll stops 的 html 元素

ios - ViewController 的释放

ios - UIViewController View 错误

ios - 实现照片浏览器 View

iOS悬停效果点击而不是点击

ios - iOS 应用程序 + 服务器集成中的 Paypal 链式支付(或自适应)

swift - 在 Realm 中保存图像