繁体   English   中英

在另一个的UIViewController初始化中初始化自定义UIViewController

[英]Initialize Custom UIViewController in another's UIViewController init

我有这些自定义的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)
    }
}

对我来说一切都很好。 不应有双重分配,因为每次视图出现时,您都将覆盖self.loadingViewController的先前分配,并且ARC将垃圾回收旧值。

不需要self.loadingViewController = LoadingViewController() ,因为此实例将不知道您使用情节self.loadingViewController = LoadingViewController()板上的IBOutlet创建的接口。 当您使用instantiateViewControllerWithIdentifier ,实例LoadingViewController与你Interface Builder的内部关联该类的UI元素创建。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM