简体   繁体   English

如何将UIViewController传递给Swift中的另一个类?

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

相关问题 Swift-如何将数据从UITableViewCell类传递到UIViewController - Swift - How to Pass Data From UITableViewCell Class to UIViewController 如何将UIViewController作为参数传递给swift 3? - How to pass UIViewController as a parameter swift 3? 如何将 function 传递给 Swift 中的另一个 class - How to pass a function to another class in Swift swift如何传递UIViewController类型的参数<XXXDelegate> - swift how to pass parameter of type UIViewController<XXXDelegate> 如何在 Swift 中的 UIViewController 和 NSObject 之间传递数据 - How to pass data between a UIViewController and an NSObject in Swift 如何在Swift中将数据从UITableViewRowAction传递到UIViewController? - How to pass data to a UIViewController from a UITableViewRowAction in Swift? 如何更新另一个UIViewController-SWIFT上的进度视图? - How to update a progress view that is on another UIViewController - SWIFT? 如何从 swift 5 中的另一个 Controller 重新加载 UIViewController - How to reload UIViewController from another Controller in swift 5 如何在另一个UIViewController中快速更改NSIndexPath - How to change NSIndexPath in another UIViewController in swift 如何从一个Containerview传递到另一个UIViewController - How to pass from one Containerview to Another UIViewController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM