gpt4 book ai didi

ios - Swift 弱委托(delegate)

转载 作者:行者123 更新时间:2023-11-30 11:27:21 26 4
gpt4 key购买 nike

我的代码:

public func start() {
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)

guard let listVC = storyboard.instantiateViewController(withIdentifier: "ListVC") as? ListVC else { return }
let viewModel = ListViewModel(dependencies: appDependencies)
viewModel.delegate = self
listVC.listViewModel = viewModel

navigationController?.pushViewController(listVC, animated: true)
}


protocol ListViewModelDelegate: class {
func needChangeScreen(cellViewModel: UserCellViewModel)
}

final class ListViewModel {

weak var delegate: ListViewModelDelegate?

func userPressed(at index: IndexPath) {
delegate?.needChangeScreen(cellViewModel: cellViewModels[index.row])
}
}

用户按下是从 UIViewController 调用的,然后我想向协调器发送回调以启动另一个协调器但委托(delegate)?始终为零。我知道委托(delegate)应该很弱,但在这种情况下对我不起作用。有什么想法吗?

最佳答案

好吧,我已经修复了,但我不知道它好吗。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let navigationController = UINavigationController()
navigationController.setNavigationBarHidden(true, animated: false)
window?.rootViewController = navigationController
let appDependencies = configureAppDependencies()
let coordinator = AppCoordinator(navigationController: navigationController, appDependencies: appDependencies)
coordinator.start()
window?.makeKeyAndVisible()

return true
}

那是我的应用程序委托(delegate)函数(在 AppCoordinator 中创建启动并推送 ListCoordinator),但是当我更改让协调器例如 var 时:

var coordinator: AppCoordinator?

弱委托(delegate)不为零,一切正常。

关于ios - Swift 弱委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50592300/

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