Ios 敏捷弱代表
我的代码:Ios 敏捷弱代表,ios,swift,mvvm,delegates,Ios,Swift,Mvvm,Delegates,我的代码: 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(depen
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调用的,然后我想向协调器发送回调以启动另一个协调器,但委托?总是零。我知道代表们应该是软弱的,但在这种情况下,这对我不起作用。有什么想法吗?好的,我有办法,但我不知道它好吗
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),但当我更改实例变量的let coordinator时:
var coordinator: AppCoordinator?
弱委托不是零,一切正常。如何调用
start
?似乎活动的ListVC
实例不是来自start
的,请确保某些东西也包含对协调器对象的引用。如果引用它的唯一对象是视图控制器委托,它是弱的
,那么它将在start
函数运行后解除分配,这将使委托为零。