1

在有用的問題Force reload watchOS 2 Complications用戶@alexeyvmp提到in a comment您應該爲CLKComplicationServerActiveComplicationsDidChangeNotification事件添加觀察者。如何應對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

你應該把觀察員InterfaceController

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

那麼你就會知道在InterfaceController時的複雜變化。