SwiftUI 五

SwiftUI 探索 - 闭包总结

SwiftUI 探索 - 状态和数据流

SwiftUI 探索 - 不透明返回类型

SwiftUI 探索 - Combine

 

SwiftUI使用UIKit框架,控制器需实现UIViewControllerRepresentable协议,视图需实现UIViewRepresentable协议

  • UIViewControllerRepresentable协议使用

    • makeUIViewController(context:) 方法当 SwiftUI 准备好显示 view 时,它会调用此方法一次,然后管理 view controller 的生命周期。

    • updateUIViewController(:context:) 方法 在其中调用 setViewControllers(:direction:animated:) 来显示当前需要显示的viewcontroller

    • coordinator:我们可以用这个 coordinator 实现常见的 Cocoa 模式,例如代理、数据源以及通过 target-action 响应用户事件。SwiftUI 会在调用 makeUIViewController(context:) 方法之前调用 makeCoordinator() 方法,这样配置 view controller 时,我们可以访问 coordinator 对象。

  • UIViewRepresentable协议使用

    • func makeUIView(context: Context)

    • func updateUIView(_ uiView: uiView, context: Context)

    • unc makeCoordinator()

  • UIHostingController 是一个 UIViewController 子类,它将负责接受一个 SwiftUI 的 View 描述并将其用 UIKit 进行渲染

猜你喜欢

转载自www.cnblogs.com/liuxiaokun/p/12676939.html