如何对CLKComplicationServerActiveComplicationsDidChangeNotification做出反应

时间:2016-08-01 10:59:16

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

在有用的问题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!")
}

感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

您应该将观察者放在 InterfaceController

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

当复杂化发生变化时,你会在 InterfaceController 中知道。