简体   繁体   English

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

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

I have these custom UIViewController, LoadingViewController and LoadableViewController and I LoadableViewController needs to present the LoadingViewController upon the startLoading function or dismiss it upon stopLoading function. 我有这些自定义的UIViewController,LoadingViewController和LoadableViewController,并且LoadableViewController需要在startLoading函数上显示LoadingViewController或在stopLoading函数上将其关闭。 My attempt is the following but I am not sure how to declare the variable for loadingViewController in the initializer, since it is already defined in the storyboard and will be allocated by the storyboard and I don't want to double allocate it for no reason (meaning add a loadingViewController = LoadingViewController() ) in the init. 我的尝试如下,但我不确定如何在初始化程序中声明用于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)
    }
}

It all looks good to me. 对我来说一切都很好。 There should be no double allocation, as each time the view appears you will overwrite the previous assignment of self.loadingViewController and ARC will garbage collect the old value. 不应有双重分配,因为每次视图出现时,您都将覆盖self.loadingViewController的先前分配,并且ARC将垃圾回收旧值。

There is no need for self.loadingViewController = LoadingViewController() , as this instance will not know about the interface you created with IBOutlet s on your storyboard. 不需要self.loadingViewController = LoadingViewController() ,因为此实例将不知道您使用情节self.loadingViewController = LoadingViewController()板上的IBOutlet创建的接口。 When you use instantiateViewControllerWithIdentifier , an instance of LoadingViewController is created with the UI elements you associated this class with within Interface Builder. 当您使用instantiateViewControllerWithIdentifier ,实例LoadingViewController与你Interface Builder的内部关联该类的UI元素创建。

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

相关问题 从 storyboard 自定义 UIViewController - Custom init of UIViewController from storyboard 从另一个UIViewController呈现UIViewController的视图 - Presenting an UIViewController's view from another UIViewController 自定义UIViewController init与init相关的数据 - Custom UIViewController init with init related data UIViewController拥有另一个UIViewController - UIViewController owns another UIViewController UIViewController不会选择另一个UIViewController吗? - UIViewController will not segue to another UIViewController? Swift 中 UIViewController 的自定义初始化,在 storyboard 中设置接口 - Custom init for UIViewController in Swift with interface setup in storyboard UISearchController 的 init(searchResultsController: UIViewController?) 崩溃 - UISearchController's init(searchResultsController: UIViewController?) crashes 是否可以将UIViewController的视图添加到另一个UIViewController的视图中? - Is it advisable to add a UIViewController's view to another UIViewController's view? 将另一个UIViewController的视图放在“主” UIViewController中—委托方法 - Putting another UIViewController's view within a “main” UIViewController — delegate methods 在另一个UIViewController中显示一个UIViewController - Displaying a UIViewController within another UIViewController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM