[英]How to pass UIViewController to another class in Swift?
How to pass UIViewController to another class in Swift? 如何将UIViewController传递给Swift中的另一个类? I have
SomeViewController
class: 我有
SomeViewController
类:
class RidingViewController: UIViewController {
fileprivate let header = RidingViewHeader(controller: self)
override func viewDidLoad() {
super.viewDidLoad()
header.setNavigationBar()
}
...
}
And I want to separate some code and to set up its header in the another class. 我想分离一些代码,并在另一个类中设置其标头。 I've created this class
我已经创建了这个课程
class RidingViewHeader {
var controller: UIViewController
let navigationBar= UINavigationBar()
let navigationItem = UINavigationItem()
init(controller: UIViewController) {
self.controller = controller
}
...
}
In this case I get an error: Cannot convert value of type '(NSObject) -> () -> UIViewController' to expected argument type 'UIViewController'
在这种情况下,我得到一个错误:
Cannot convert value of type '(NSObject) -> () -> UIViewController' to expected argument type 'UIViewController'
What is the better way of doing it? 有什么更好的方法呢?
You can't access self
until the view controller has been initialized. 在初始化视图控制器之前,您无法访问
self
。 You could make it a lazy variable: 您可以将其设置为惰性变量:
fileprivate lazy var header: RidingViewHeader = {
return RidingViewHeader(controller: self)
}()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.