如何在默认的通知中心中添加一个观察者?我试图移植这行代码,当电池电量变化时发送通知。
[[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交互
其他回答
创建一个objc函数,当通知被调用时执行该函数。
@objc func reloadNotification(_ notification: Notification) {
tblview.reloadData()
}
在didload视图中添加通知观察者。
NotificationCenter.default.addObserver(self, selector:#selector(reloadNotification(_:)), name: Notification.Name("reloadSideMenuDataNS"),object: nil)
将通知发布到想要调用函数的位置。
NotificationCenter.default.post(name: Notification.Name("reloadSideMenuDataNS"), object: nil)
您可以删除您的通知在视图已消失下面。
NotificationCenter.default.removeObserver(self, name: Notification.Name("reloadSideMenuDataNS"), 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交互
我们也应该删除通知。
Ex.
deinit
{
NotificationCenter.default.removeObserver(self, name:NSNotification.Name(rawValue: "notify"), object: nil)
}
Swift 4.0 & Xcode 9.0+:
发送(Post)通知:
NotificationCenter.default.post(name: Notification.Name("NotificationIdentifier"), object: nil)
OR
NotificationCenter.default.post(name: Notification.Name("NotificationIdentifier"), object: nil, userInfo: ["Renish":"Dadhaniya"])
得到(获得)通知:
NotificationCenter.default.addObserver(self, selector: #selector(self.methodOfReceivedNotification(notification:)), name: Notification.Name("NotificationIdentifier"), object: nil)
接收到的通知的函数方法处理程序:
@objc func methodOfReceivedNotification(notification: Notification) {}
Swift 3.0 & Xcode 8.0+:
发送(Post)通知:
NotificationCenter.default.post(name: Notification.Name("NotificationIdentifier"), object: nil)
得到(获得)通知:
NotificationCenter.default.addObserver(self, selector: #selector(YourClassName.methodOfReceivedNotification(notification:)), name: Notification.Name("NotificationIdentifier"), object: nil)
接收到的通知的方法处理程序:
func methodOfReceivedNotification(notification: Notification) {
// Take Action on Notification
}
删除通知:
deinit {
NotificationCenter.default.removeObserver(self, name: Notification.Name("NotificationIdentifier"), object: nil)
}
Swift 2.3和Xcode 7:
发送(Post)通知
NSNotificationCenter.defaultCenter().postNotificationName("NotificationIdentifier", object: nil)
得到(获得)通知
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(YourClassName.methodOfReceivedNotification(_:)), name:"NotificationIdentifier", object: nil)
接收的通知的方法处理程序
func methodOfReceivedNotification(notification: NSNotification){
// Take Action on Notification
}
对于历史上的Xcode版本…
发送(Post)通知
NSNotificationCenter.defaultCenter().postNotificationName("NotificationIdentifier", object: nil)
得到(获得)通知
NSNotificationCenter.defaultCenter().addObserver(self, selector: "methodOfReceivedNotification:", name:"NotificationIdentifier", object: nil)
删除通知
NSNotificationCenter.defaultCenter().removeObserver(self, name: "NotificationIdentifier", object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self) // Remove from all notifications being observed
接收的通知的方法处理程序
func methodOfReceivedNotification(notification: NSNotification) {
// Take Action on Notification
}
用@objc注释类或目标方法
@objc private func methodOfReceivedNotification(notification: NSNotification) {
// Take Action on Notification
}
// Or
dynamic private func methodOfReceivedNotification(notification: NSNotification) {
// Take Action on Notification
}
使用NSNotificationCenter传递数据
也支持通过swift 3.0中的NotificationCentre和swift 2.0中的NSNotificationCenter进行数据传递。
Swift 2.0版本
使用userInfo传递信息,userInfo是一个可选的字典类型[NSObject: AnyObject]?
let imageDataDict:[String: UIImage] = ["image": image]
// Post a notification
NSNotificationCenter.defaultCenter().postNotificationName(notificationName, object: nil, userInfo: imageDataDict)
// Register to receive notification in your class
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.showSpinningWheel(_:)), name: notificationName, object: nil)
// handle notification
func showSpinningWheel(notification: NSNotification) {
if let image = notification.userInfo?["image"] as? UIImage {
// do something with your image
}
}
Swift 3.0版本
userInfo现在需要[AnyHashable:Any]?作为一个参数,我们在Swift中作为字典字面量提供
let imageDataDict:[String: UIImage] = ["image": image]
// post a notification
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "notificationName"), object: nil, userInfo: imageDataDict)
// `default` is now a property, not a method call
// Register to receive notification in your class
NotificationCenter.default.addObserver(self, selector: #selector(self.showSpinningWheel(_:)), name: NSNotification.Name(rawValue: "notificationName"), object: nil)
// handle notification
func showSpinningWheel(_ notification: NSNotification) {
if let image = notification.userInfo?["image"] as? UIImage {
// do something with your image
}
}
使用NotificationCentre(swift 3.0)和NSNotificationCenter(swift 2.0)传递源数据