我想在 View Controller 调用viewDidLoad之前做一些事情。我调用 loadViewIfNeeded() 但 self.navigationController 仍然为零。我怎样才能加载它?
AppDelegate.swift
func someMethod() {
var viewController = Storyboard.instantiate()
let viewModel = SomeViewModel()
vc.bind(to: viewModel)
}
BindableType.swift
protocol BindableType {
associatedtype ViewModelType
var viewModel: ViewModelType! { get set }
func bindViewModel()
}
extension BindableType where Self: UIViewController {
mutating func bind(to model: Self.ViewModelType) {
viewModel = model
loadViewIfNeeded()
// PROBLEM HERE: navigationController is nil, but view have been loaded
navigationController?.navigationBar.prefersLargeTitles = true
navigationController?.navigationItem.largeTitleDisplayMode = .automatic
bindViewModel()
}
}
SomeViewController.swift
class SomeViewController: BindableType {
override func viewDidLoad() {
super.viewDidLoad()
// navigationController?.navigationBar.prefersLargeTitles = true
// navigationController?.navigationItem.largeTitleDisplayMode = .automatic
}
func bindViewModel() {
...
}
}
最佳答案
我发现了问题。我必须将带有推送 Controller 的行放在 viewController.bind(to: viewModel)
之上self.navigationViewController.pushViewController(viewController, animated: true)
viewController.bind(to: viewModel)
关于swift - 如何在调用 `viewDidLoad` 之前加载 self.navigationController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50747085/