如何在默认的通知中心中添加一个观察者?我试图移植这行代码,当电池电量变化时发送通知。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryLevelChanged:) name:UIDeviceBatteryLevelDidChangeNotification object:nil];
如何在默认的通知中心中添加一个观察者?我试图移植这行代码,当电池电量变化时发送通知。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryLevelChanged:) name:UIDeviceBatteryLevelDidChangeNotification object:nil];
当前回答
它与Objective-C API相同,但使用了Swift的语法。
Swift 4.2和Swift 5:
NotificationCenter.default.addObserver(
self,
selector: #selector(self.batteryLevelChanged),
name: UIDevice.batteryLevelDidChangeNotification,
object: nil)
如果你的观察者没有从Objective-C对象继承,你必须用@objc作为你的方法的前缀来使用它作为选择器。
@objc private func batteryLevelChanged(notification: NSNotification){
//do stuff using the userInfo property of the notification object
}
参见NSNotificationCenter类参考,与Objective-C api交互
其他回答
这是一个非常简单的自定义通知观察者和post的例子
添加通知观察员
NotificationCenter.default.addObserver(self, selector: #selector(myFunction), name: Notification.Name("CustomeNotificationName"), object: nil)
添加选择器并处理观察者调用
@objc func myFunction(notification: Notification) {
//Write you code
}
在需要时发布通知(观察员)。
NotificationCenter.default.post(name: NSNotification.Name("CustomeNotificationName"), object: "Object", userInfo: ["key":"Value"])
注意:-当你离开屏幕时,你需要删除观察者。如。
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self);
}
Declare a notification name extension Notification.Name { static let purchaseDidFinish = Notification.Name("purchaseDidFinish") } You can add observer in two ways: Using Selector NotificationCenter.default.addObserver(self, selector: #selector(myFunction), name: .purchaseDidFinish, object: nil) @objc func myFunction(notification: Notification) { print(notification.object ?? "") //myObject print(notification.userInfo ?? "") //[AnyHashable("key"): "Value"] } or using block NotificationCenter.default.addObserver(forName: .purchaseDidFinish, object: nil, queue: nil) { [weak self] (notification) in guard let strongSelf = self else { return } strongSelf.myFunction(notification: notification) } func myFunction(notification: Notification) { print(notification.object ?? "") //myObject print(notification.userInfo ?? "") //[AnyHashable("key"): "Value"] } Post your notification NotificationCenter.default.post(name: .purchaseDidFinish, object: "myObject", userInfo: ["key": "Value"])
iOS 9和OS X 10.11。它不再需要一个 NSNotificationCenter观察者取消注册 收回。更多信息
对于基于块的实现,如果你想在块中使用self,你需要做一个弱-强舞蹈。更多信息
基于块的观察者需要删除更多信息
let center = NSNotificationCenter.defaultCenter()
center.removeObserver(self.localeChangeObserver)
Swift 5和Xcode 10.2:
NotificationCenter.default.addObserver(
self,
selector: #selector(batteryLevelDidChangeNotification),
name: UIDevice.batteryLevelDidChangeNotification,
object: nil)
在Swift 5中
我们说如果想从ViewControllerB接收数据到 ViewControllerA
ViewControllerA (Receiver)
import UIKit
class ViewControllerA: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//MARK: - - - - - Code for Passing Data through Notification Observer - - - - -
// add observer in controller(s) where you want to receive data
NotificationCenter.default.addObserver(self, selector: #selector(self.methodOfReceivedNotification(notification:)), name: Notification.Name("NotificationIdentifier"), object: nil)
}
//MARK: - - - - - Method for receiving Data through Post Notificaiton - - - - -
@objc func methodOfReceivedNotification(notification: Notification) {
print("Value of notification : ", notification.object ?? "")
}
}
视图控制器 B (发射器)
import UIKit
class ViewControllerB: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//MARK: - - - - - Set data for Passing Data Post Notification - - - - -
let objToBeSent = "Test Message from Notification"
NotificationCenter.default.post(name: Notification.Name("NotificationIdentifier"), object: objToBeSent)
}
}
Swift 5通知观察员
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(batteryLevelChanged), name: UIDevice.batteryLevelDidChangeNotification, object: nil)
}
@objc func batteryLevelChanged(notification : NSNotification){
//do here code
}
override func viewWillDisappear(_ animated: Bool) {
NotificationCenter.default.removeObserver(self, name: UIDevice.batteryLevelDidChangeNotification, object: nil)
}