[英]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.