簡體   English   中英

迅捷的弱代表

[英]Swift weak delegate

我的代碼:

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調用的,然后我想將回調發送給Coordinator以啟動另一個協調器,但要委派嗎? 永遠是零。 我知道代表們應該軟弱無力,但在這種情況下對我來說不起作用。 有任何想法嗎?

Okey我已經解決了,但我不知道這是好事。

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
}

那是我的應用程序委托函數(在AppCoordinator中,開始創建並推送ListCoordinator),但是當我更改時,讓協調器例如var:

var coordinator: AppCoordinator?

弱代表不是零,一切正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM