gpt4 book ai didi

swift - 如何在调用 `viewDidLoad` 之前加载 self.navigationController

转载 作者:行者123 更新时间:2023-11-30 11:25:57 28 4
gpt4 key购买 nike

我想在 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com