nsnotificationcenter

NSNotificationCenter addObserver in Swift

独自空忆成欢 提交于 2019-11-25 23:06:58
问题 How do you add an observer in Swift to the default notification center? I\'m trying to port this line of code that sends a notification when the battery level changes. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryLevelChanged:) name:UIDeviceBatteryLevelDidChangeNotification object:nil]; 回答1: It's the same as the Objective-C API, but uses Swift's syntax. NSNotificationCenter.defaultCenter().addObserver( self, selector: #selector(batteryLevelChanged:), name:

Send and receive messages through NSNotificationCenter in Objective-C?

一笑奈何 提交于 2019-11-25 21:49:44
问题 I am attempting to send and receive messages through NSNotificationCenter in Objective-C. However, I haven\'t been able to find any examples on how to do this. How do you send and receive messages through NSNotificationCenter ? 回答1: @implementation TestClass - (void) dealloc { // If you don't remove yourself as an observer, the Notification Center // will continue to try and send notification objects to the deallocated // object. [[NSNotificationCenter defaultCenter] removeObserver:self];