如何在默认的通知中心中添加一个观察者?我试图移植这行代码,当电池电量变化时发送通知。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryLevelChanged:) name:UIDeviceBatteryLevelDidChangeNotification object:nil];

当前回答

在swift 3, Xcode 8.2:-检查电池状态水平

//Add observer
NotificationCenter.default.addObserver(self, selector: #selector(batteryStateDidChange), name: NSNotification.Name.UIDeviceBatteryStateDidChange, object: nil)


 //Fired when battery level changes

 func batteryStateDidChange(notification: NSNotification){
        //perform manipulation here
    }

其他回答

在swift 3, Xcode 8.2:-检查电池状态水平

//Add observer
NotificationCenter.default.addObserver(self, selector: #selector(batteryStateDidChange), name: NSNotification.Name.UIDeviceBatteryStateDidChange, object: nil)


 //Fired when battery level changes

 func batteryStateDidChange(notification: NSNotification){
        //perform manipulation here
    }

创建一个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)

NSNotificationCenter在iOS 11的Swift 4.0中添加了观察者语法

  NotificationCenter.default.addObserver(self, selector: #selector(keyboardShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)

这是为keyboardWillShow通知名称类型。其他类型可以从可用选项中选择

选择器的类型是@objc func,它处理键盘的显示方式(这是你的用户函数)

它与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交互

在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)
    }

}