在另一个UIViewController init中初始化Custom UIViewController

时间:2015-09-08 18:51:44

标签: ios iphone swift uiviewcontroller uistoryboard

我有这些自定义UIViewController,LoadingViewController和LoadableViewController,我的LoadableViewController需要在startLoading函数上显示LoadingViewController或在stopLoading函数时将其解除。我的尝试是以下但我不知道如何在初始化程序中声明loadViewController的变量,因为它已经在故事板中定义并将由故事板分配,我不想将其重复分配为否原因(意思是在init中添加一个loadingViewController = LoadingViewController())。

import UIKit

class LoadableViewController: UIViewController {

    var loadingViewController: LoadingViewController

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func viewDidAppear(animated: Bool) {
        loadingViewController = storyboard?.instantiateViewControllerWithIdentifier("LoadingiewController") as! LoadingViewController
    }

    func stopLoading() {
        loadingViewController.dismissViewControllerAnimated(true, completion: nil)
    }

    func startLoading() {
        presentViewController(loadingViewController, animated: true, completion: nil)
    }
}

1 个答案:

答案 0 :(得分:0)

这对我来说都很好看。应该没有双重分配,因为每次出现视图时,您将覆盖先前的self.loadingViewController分配,ARC将垃圾收集旧值。

不需要self.loadingViewController = LoadingViewController(),因为此实例不会了解您在故事板上使用IBOutlet创建的界面。当您使用instantiateViewControllerWithIdentifier时,将使用您在Interface Builder中与此类关联的UI元素创建LoadingViewController的实例。