0

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.

Mahak Mittal
  • 121
  • 1
  • 10
  • add `self.` in front of `closButtonPressed(notification:)` in the `addObserver` Line – iOSer Dec 14 '18 at 05:49
  • tried. Does not work – Mahak Mittal Dec 14 '18 at 05:51
  • Hmmm just to be sure. The `addObserver` as well as `closButtonPressed` are in Controller A. While, `post` is in Controller B? – iOSer Dec 14 '18 at 05:53
  • Also try using raw values while naming Notification ala `NSNotification.Name(rawValue: "CloseButtonPressed")` – iOSer Dec 14 '18 at 05:55
  • 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: https://github.com/aFrogleap/SimpleImageViewer – Mahak Mittal Dec 14 '18 at 05:56
  • Tried with raw values as well. Same result. does not get called – Mahak Mittal Dec 14 '18 at 05:58
  • Hmmm weird!! Just ensure that the notification is not getting removed unknowingly. Don't see why using a third party framework would hinder with the basic implementations of swift. If possible to share the project, then I can look into it? – iOSer Dec 14 '18 at 06:05
  • If you are dismissing vc2 and coming back to vc1, and want to pass value from vc2 to vc1, then using notification approach is totally wrong in terms of memory. You should either use protocol delegate for this purpose OR need to use completionHandler for this purpose. This is to improve your code and get into right direction of programming. @MahakMittal, Hope you understand this – Mehul Thakkar Dec 14 '18 at 06:17
  • Thanks for support guys. But there is some threading issue. It does get called when using from main thread. thanks for help – Mahak Mittal Dec 14 '18 at 06:21
  • @Mehul Yes I understand. Thanks for suggestion – Mahak Mittal Dec 14 '18 at 06:21

3 Answers3

0

Make sure posting notification in completion handler

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

}

0

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)
Lal Krishna
  • 15,485
  • 6
  • 64
  • 84
0

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?

Kirinzer
  • 11
  • 4