如何将控制器作为inout参数传递?

时间:2019-08-27 10:05:06

标签: ios swift

我只是有这样的协议:

protocol Containerable {
    var containerView: UIView { get }
    var containerController: UIViewController { get }
    var oldViewController: UIViewController? { get set }
}
protocol ContainerRoutable: class {
    func load(controller: UIViewController, into context: inout Containerable)
}
extension ContainerRoutable {
    func load(controller: UIViewController, into context: inout Containerable) {
        context.oldViewController?.willMove(toParent: nil)
        context.oldViewController?.view.removeFromSuperview()
        context.oldViewController?.removeFromParent()
        controller.view.frame = context.containerView.bounds
        context.containerController.addChild(controller)
        context.containerView.addSubview(controller.view)
        context.oldViewController = controller
        controller.didMove(toParent: context.containerController)
    }
    func loadDashboard(into context: inout Containerable) {
        let controller = assembler.resolve(DashboardViewController.self)!
        load(controller: controller, into: &context)
    }
}

现在我需要使用点击操作:

        mainView.dashboardButton.rx.tap.bind { [weak self] in
            self?.mainView.activateDashboardMenuItem()
            if var a = self as? Containerable { //warning: Conditional downcast from 'TabBarController?' to 'Containerable' is equivalent to an implicit conversion to an optional 'Containerable'
                self?.router.loadDashboard(into: &a)
            }
            }.disposed(by: bag)

什么是自我?

class TabBarController: UIViewController, Containerable {
    private let mainView: TabBarView
    let router: TabBarRoutable
    private let bag = DisposeBag()
    var oldViewController: UIViewController?
    var containerController: UIViewController {
        return self
    }
    var containerView: UIView {
        return mainView.containerView
    }
}

如何删除以下警告?

Conditional downcast from 'TabBarController?' to 'Containerable' is equivalent to an implicit conversion to an optional 'Containerable'

1 个答案:

答案 0 :(得分:0)

将if条件更新为

if var a: Containerable = self {
     self?.router.loadDashboard(into: &a)
}