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

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

当前回答

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

其他回答

我们也应该删除通知。

Ex.

deinit 
{
  NotificationCenter.default.removeObserver(self, name:NSNotification.Name(rawValue: "notify"), object: nil)

}

Xcode 8中的Swift 3.0

Swift 3.0已经用结构“包装器类型”取代了许多“字符串类型”的api, NotificationCenter也是如此。通知现在由struct notification标识。名称而不是字符串。参见迁移到Swift 3指南。

以前的用法:

// Define identifier
let notificationIdentifier: String = "NotificationIdentifier"

// Register to receive notification
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(YourClassName.methodOfReceivedNotification(_:)), name: notificationIdentifier, object: nil)

// Post a notification
NSNotificationCenter.defaultCenter().postNotificationName(notificationIdentifier, object: nil)

Swift 3.0的新用法:

// Define identifier
let notificationName = Notification.Name("NotificationIdentifier")

// Register to receive notification
NotificationCenter.default.addObserver(self, selector: #selector(YourClassName.methodOfReceivedNotification), name: notificationName, object: nil)

// Post notification
NotificationCenter.default.post(name: notificationName, object: nil)

所有系统通知类型现在都定义为notification . name上的静态常量;例如。uidevicebatteryleveldidchange, . uiapplicationdidfinishlaunching, . uitextfieldtextdidchange等。

您可以扩展通知。使用您自己的自定义通知命名,以便与系统通知保持一致:

// Definition:
extension Notification.Name {
    static let yourCustomNotificationName = Notification.Name("yourCustomNotificationName")
}

// Usage:
NotificationCenter.default.post(name: .yourCustomNotificationName, object: nil)

这是一个非常简单的自定义通知观察者和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);
}

在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 5和Xcode 10.2:

NotificationCenter.default.addObserver(
            self,
            selector: #selector(batteryLevelDidChangeNotification),
            name: UIDevice.batteryLevelDidChangeNotification,
            object: nil)