ios - WatchKit:将数据从第三个 Controller 传递到初始 Controller

标签 ios swift watchkit

我有一个具有三个 Controller 的 watch 应用程序。主 Controller ,另一个带有 TableView 的 Controller ,最后一个是设置 Controller 。在设置 Controller 中我有一个开关。当它的值发生更改时,该值将存储到 bool 变量中。我想将此变量的值发送到第一个 Controller ,这样当我滑回第一个 Controller 时,该值将被发送到那里。是否可以?我怎样才能做到这一点?

最佳答案

是的,这是可能的。您必须使用contextForSegueWithIdentifier。您必须从此方法返回数据,并在目标类(在您的情况下为“第一个 Controller ”)中访问该数据。

你可以这样实现:

// In settings controller

override func contextForSegueWithIdentifier(segueIdentifier: String) -> AnyObject? {

    return self.currentValue

// In first controller
override func awakeWithContext(context: AnyObject?) {
    super.awakeWithContext(context)

    if let value: String = context as? String {
        print(value)

}

关于ios - WatchKit:将数据从第三个 Controller 传递到初始 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32137864/

相关文章:

ios - 缺少推送通知权利

ios - 关闭时将值传递给 View Controller 时出错 | swift 5

ios - 如何在不使用 UINavigationController 的情况下以编程方式进入 rootViewController

swift - 从 Parse 检索到所有响应后执行操作

swift - 如何根据 WatchKit 中的 watch 大小缩放字体大小?

iOS:具有隐藏 View 的布局约束

ios - Realm 文件管理

swift - 在 Swift 3 中使用 NSHashTable 实现观察者模式

ios - 苹果 watch CMDeviceMotion 没有给我很好的读数

ios - Apple 是否将 NSUserDefaults 共享从 iOS 应用更改为 watchOS 应用