Notification usage in ios development

1. Define notificationKey

let lightNotificationKey = "co.seanallen.lightSide"

let darkNotificationKey = "co.seanallen.darkSide"

2. Registration message


    let light = Notification.Name(rawValue: lightNotificationKey)

    let dark = Notification.Name(rawValue: darkNotificationKey)


//Light

        NotificationCenter.default.addObserver(self, selector: #selector(BaseScreen.updateCharacterImage(notification:)), name: light, object: nil)

        NotificationCenter.default.addObserver(self, selector: #selector(BaseScreen.updateNameLabel(notification:)), name: light, object: nil)

        NotificationCenter.default.addObserver(self, selector: #selector(BaseScreen.updateBackground(notification:)), name: light, object: nil)

The third parameter is the registered message, which will be received when the message is sent elsewhere.

3. Send a message

let name = Notification.Name(rawValue: darkNotificationKey)

        NotificationCenter.default.post(name: name, object: nil)

        dismiss(animated: true, completion: nil)

name is the initialized message.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325542116&siteId=291194637