0
votes

I am adding an observer on A view controller and view controller B is presenting on A.

While dismissing Controller B , I have posted the notification but It does not called the selector method added in A.

Also notification get registered first and the post method is get called. I have already checked that.

Here is the sample code:

NotificationCenter.default.addObserver(self, selector: #selector(closButtonPressed(notification:)) ,name: Notification.Name("CloseButtonPressed"), object: nil)


@objc func closButtonPressed(notification: Notification){
}

NotificationCenter.default.post(name: Notification.Name("CloseButtonPressed"), object: self)

Any help would be appreciated.

3
add self. in front of closButtonPressed(notification:) in the addObserver Line - iOSer
tried. Does not work - Mahak Mittal
Hmmm just to be sure. The addObserver as well as closButtonPressed are in Controller A. While, post is in Controller B? - iOSer
Also try using raw values while naming Notification ala NSNotification.Name(rawValue: "CloseButtonPressed") - iOSer
Yes it is. I think there is something related to presentation. I am using a third party image viewer to show image. And when dismissing it I need an action. Here is the third party link: github.com/aFrogleap/SimpleImageViewer - Mahak Mittal

3 Answers

0
votes

Make sure posting notification in completion handler

self?.dismiss(animated: true, completion: {
NotificationCenter.default.post(name: Notification.Name("CloseButtonPressed"), 
object: self)

}

0
votes

Make sure you implement Notification correctly. I recommend you to create an extension for Notification Name & make it static.

extension Notification.Name {
    static let didTapCloseButton = Notification.Name("CloseButtonPressed")
}

NotificationCenter.default.addObserver(self, selector: #selector(didTapCloseButton(_:)), name: .didTapCloseButton, object: nil)

@objc func didTapCloseButton(_ sender: Notification?) {

}

NotificationCenter.default.post(name: .didTapCloseButton, object: nil)
0
votes

I am not sure what's wrong with your project. To solve your problem, I create a test project and write some code like this:

    //ControllerA.swift
    override func viewDidLoad() {
    NotificationCenter.default.addObserver(self, selector: #selector(getNotification(notification:)), name:      NSNotification.Name("CloseButtonPressed"), object: nil)
    }

    getNotification(notification: Notification) {
    print(notification)
    }

    @objc func buttonAClick() {
        navigationController?.present(ViewControllerB(), animated: true, completion: {
        })
    }

    //ViewControllerB.swift
    @objc func buttonClick() {
            NotificationCenter.default.post(name: Notification.Name("CloseButtonPressed"), object: self)
            self.dismiss(animated: true) {
            }
        }

As you said, I add notification in ControllerA, and present ControllerB, When ControllerB close, post notification and dismiss, console can print the notification object, so may I miss something?