【问题标题】:How to react to CLKComplicationServerActiveComplicationsDidChangeNotification如何对 CLKComplicationServerActiveComplicationsDidChangeNotification 做出反应
【发布时间】:2016-08-01 10:59:16
【问题描述】:

在有用的问题Force reload watchOS 2 Complications 用户@alexeyvmp 提到in a comment,您应该为CLKComplicationServerActiveComplicationsDidChangeNotification 事件添加观察者。

创建这个观察者的好地方是什么,它会是什么样子?我是从我的 ComplicationDataSource 还是在我的 InterfaceController 中创建它?如何确保它不会一遍又一遍地重新创建?

我试图在Swift 中阅读有关如何创建观察者的信息,但我很难弄清楚将其放在哪里。我目前有

let notificationCenter = NSNotificationCenter.defaultCenter()
let mainQueue = NSOperationQueue.mainQueue()

_ = notificationCenter.addObserverForName(CLKComplicationServerActiveComplicationsDidChangeNotification, object: nil, queue: mainQueue) { _ in
    print("active complications changed. refresh!")
}

感谢任何帮助。

【问题讨论】:

    标签: swift apple-watch nsnotificationcenter apple-watch-complication clockkit


    【解决方案1】:

    你应该把观察者放在InterfaceController中:

    NotificationCenter.default.addObserver( self,
            selector: #selector(InterfaceController.CLKComplicationServerActiveComplicationsDidChangeNotification(_:)),
            name: NSNotification.Name(rawValue: "CLKComplicationServerActiveComplicationsDidChangeNotification"), object: nil )
    

    所以你会在InterfaceController中知道并发症何时发生变化。

    【讨论】:

      猜你喜欢
      • 2021-02-18
      • 2021-05-12
      • 1970-01-01
      • 2017-11-08
      • 2010-09-20
      • 2021-03-05
      • 1970-01-01
      • 1970-01-01
      • 2022-12-24
      相关资源
      最近更新 更多