我的理解可能是作业的意图吗?
没有。如果您使用的是 MVVM,我宁愿将字符串值设置为 viewModel,并将完全配置的 viewModel 作为 ViewController 的依赖项注入(称为控制反转原则(IoC))。
如果你不使用任何第三方依赖注入器,你总是可以使用init 这称为Dependency injection via Constructor
引入依赖注入的方法有多种,本答案不在讨论范围内。
下面的答案假设您没有使用任何第三方依赖注入器并使用基于Constructor 的注入。
如何从 SceneDelegate 实例化这个视图控制器?
第 1 步:有一个 viewModel 类
class ViewModelA {
let someParam: String
init(with param: String) {
self.someParam = param
//rest of code
}
//rest of code whatever makes sense here
//modification of string any buisness logic
//or hold other data models
}
第二步:将ViewModel作为构造函数注入ViewController
class ViewControllerA: UIViewController {
let viewModel: ViewModelA
init(with viewModel: ViewModelA) {
self.viewModel = viewModel
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//rest of code to set up, update UI and view delegates
}
第三步:在SceneDelegate的 willConnectTo session方法中将你的视图控制器设置为窗口的根视图控制器
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
guard let windowScene = (scene as? UIWindowScene) else { return }
let window = UIWindow(windowScene: windowScene)
let viewModelA = ViewModelA(with: "abcd")
let viewControllerA = ViewControllerA(with: viewModelA)
window.rootViewController = viewControllerA
self.window = window
window.makeKeyAndVisible()
}
我采用了 MVVM 模式,场景委托看起来不像
离开视图模型的好地方。
你必须在某个地方设置 rootView 控制器,你不觉得吗?之前我们曾经在 AppDelegate 中设置 rootView 控制器(从概念上讲,在 AppDelegate 中创建 ViewModel 和 ViewController 实例也有些不稳定)。即使您使用故事板并将某些视图控制器设置为初始视图控制器,即使 iOS 在后台也会做同样的事情。因为你想遵循 MVVM 而不是 MVC,你必须手动拦截和实例化 ViewController,并将 viewModel 作为其依赖项。
我个人更喜欢使用带有路由器的 MVVM,这样可以将创建和注入 ViewController 的依赖项分离出来(更好的关注点分离)并且可以重用(更好的代码重用性)