Change the content of a View from UISceneDelegate

I have the following view.

struct ContentView: View {
@State var data = "Intitial Value"

var body: some View {
    VStack {
        Text(data)
        Button(action: { self.data="Changed value" }) {
            Text("Click Here")
        }
    }
}
}

This simple code works fine, whenever I click the button the data property it reflects on the screen label.

Now I want to change the text label whenever I receive an URL and the following code doesn't work.

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    guard let vc = self.window?.rootViewController as? UIHostingController<ContentView> else  {
        return
    }
    vc.rootView.data = "Scene Delegate"
}

Upvotes: 1

Views: 70

Answers (1)

Asperi
Asperi

Reputation: 257493

You can do this using model view injected via EnvironmentObject

class DataViewModel: ObservableObject {
    @Published var data = "Intitial Value"
}

struct ContentView: View {
    @EnvironmentObject var vm: DataViewModel       // < declare
    var body: some View {
        VStack {
            Text(vm.data)                          // < use
            Button(action: { 
                self.vm.data="Changed value"       // < use
            }) {
                Text("Click Here")
            }
        }
     }
}

and in SceneDelegate, have this as member

class SceneDelegate {
   var dataVM = DataViewModel()         // << create

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
      ...

       let contentView = ContentView()
                             .environmentObject(dataVM)     // << inject
        window?.rootViewController = UIHostingController(rootView: contentView)
      ...
    }


    func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
        dataVM.data = "Scene Delegate"     // << modify
    }

    ...

Upvotes: 1

Related Questions