2

Currently I'm the one not getting it.

Problem:

Trying to connect to a View Method from a Scene Delegate e.g.:

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
  var window: UIWindow?

  ...

  func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
    // guard let contentView = ???? as? ContentView else { return }
    // contentView.callMethod(parameter: true)
  }
}
struct ContentView: View {    
  var body: some View {
    ...
  }

  func callMethode(parameter: Bool) {
    print("called")
  }
}

Any clue how to connect to the View and call a method in-between?

thx Jo

1
  • Just don't do it. The communication between views are supposed through data, or combine framework. Don't call the view directly. Commented Nov 2, 2019 at 3:13

2 Answers 2

1

This is counter to the design of the SwiftUI framework. You should not have any persistent view around to call methods on. Instead, views are created and displayed as needed in response to your app's state changing.

For example, if your SceneDelegate had a reference to an instance of a model class, and your view depended on that model, you could modify the model in scene(_:continue:) and your view would update automatically.

If you can provide more specifics on what you're attempting to do, I may be able to give a more specific answer.

Sign up to request clarification or add additional context in comments.

Comments

0

Many thanks. Fully understood... Will read the docs...

So is this a possible way as an example:

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?
    let activity = UserActivityManager()

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

        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: contentView.environmentObject(activity))
            self.window = window
            window.makeKeyAndVisible()

            if let userActvity = connectionOptions.userActivities.first {
                guard let title = userActvity.title else { return }
                activity.doAction(action: title)
            }
        }
    }

    func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
        guard let title = userActivity.title else { return }
        activity.doAction(action: title)
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.