【问题标题】:WatchOS: Pass data from one controller to another through "Next Page" segueWatchOS:通过“下一页”segue 将数据从一个控制器传递到另一个控制器
【发布时间】:2020-04-01 17:27:49
【问题描述】:

我正在使用 WKInterfaceController.reloadRootControllers 在我的应用程序中加载 3 个带有上下文的接口控制器。

一切正常。但是,我需要在滑动时将数据从第 1 页传递到第 2 页。似乎没有任何问题可以让我以编程方式使用它来创建新上下文或以另一种方式传递数据。

我该如何解决这个问题? 我不想为此创建一个单例。我知道如何在其他 segues 的上下文中传递数据,这个问题特别与 Watchkit 上的“下一页”导航有关。到目前为止我找不到答案。

谢谢!

马库斯

【问题讨论】:

标签: swift segue watchkit apple-watch watchos


【解决方案1】:

您是否尝试过在第一页中使用 contextForSegue?

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

【讨论】:

  • 这仅适用于推送或模态segues。如果您使用基于页面的导航,则不会调用 contextForSegue。我有同样的问题,还没有找到任何解决方案,所以我正在使用 ExtensionDelegate 解决它。
【解决方案2】:

我也一直在寻找这个问题的答案..

NextPage Segue 类似于 iOS 中的UIPageViewController。我已经尝试过contextForSegue(withIdentifier segueIdentifier: String),但它不会在滑动时被调用,而且你不能在故事板中给这个 segue 一个identifier

对我来说唯一的解决方法是使用NotificationCenter

在第一个 InterfaceController 中,我在 willDisappear/didDeactivate 上发布了一个带有对象的通知,这对 watchOS 和我订阅的第二个 InterfaceController 没有真正的影响。

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

添加我添加了这个函数

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

最后别忘了移除观察者

deinit {
  NotificationCenter.default.removeObserver(self)
}

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-11
    • 1970-01-01
    • 1970-01-01
    • 2017-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多