Using environmentObject in watchOS

前端 未结 3 1690
盖世英雄少女心
盖世英雄少女心 2021-02-05 05:23

I am trying to use environmentObject in a watchOS6 app to bind my data model to my view.

I have created a simple, stand-alone Watch app in Xcode 11.

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-05 06:08

    Adding to Matteo's awesome answer,

    If you want to use delegate then use like this:

    class HostingController : WKHostingController {
        override var body: AnyView {
            var contentView = ContentView()
            contentView.environmentObject(DataModel())
            contentView.delegate = self
            let contentWrapperView = AnyView(contentView)
            return contentWrapperView
        }
    }
    

提交回复
热议问题