WatchOS: Pass data from one controller to another through "Next Page" segue

Viewed 875

I am loading 3 interfacecontrollers with WKInterfaceController.reloadRootControllers with contexts in my app.

Everything works just fine. However, I need to pass data from Page 1 to Page 2 on swipe. It seems there is not segue that I can use programmatically to create a new context or pass data another way.

How could I solve this? I don't want to create a singleton just for this. I know how to pass data in contexts with other segues, this question specifically relate to "Next Page" navigation on Watchkit. I couldn't find an answer so far.

Thanks!

Markus

2 Answers

Have you tried using contextForSegue in your first page?

override func contextForSegue(withIdentifier segueIdentifier: String) -> Any? {
    let myDate = "today"
    return myDate
}

I'm been looking around for an answer for this question also..

NextPage Segue is similar to UIPageViewController in iOS. I have tried contextForSegue(withIdentifier segueIdentifier: String) but it is not getting called on swipe, and you can't give this segue an identifier in the storyboard.

The only way around it for me was to use NotificationCenter

In the first InterfaceController I post a notification with the object on willDisappear/didDeactivate doesn't really make a difference with watchOS and on the second InterfaceController I subscribed to it.

NotificationCenter.default.post(name: Notification.Name("interface1WillDisappear"), object: context)
NotificationCenter.default.addObserver(self, selector: #selector(awake(withNotification:)), name: Notification.Name("interface1WillDisappear"), object: nil)

Add I added this func

func awake(withNotification notification: Notification) {
  guard let context = context as? MyContextObject else { return }
  print(context)
}

Finally don't forget to remove the observer

deinit {
  NotificationCenter.default.removeObserver(self)
}

Hope this helps!

Related