NSNotificationCenter addObserver in Swift

后端 未结 14 2002
难免孤独
难免孤独 2020-11-22 05:23

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.

14条回答
  •  礼貌的吻别
    2020-11-22 06:14

    NSNotificationCenter add observer syntax in Swift 4.0 for iOS 11

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

    This is for keyboardWillShow notification name type. Other type can be selected from the available option

    the Selector is of type @objc func which handle how the keyboard will show ( this is your user function )

提交回复
热议问题