PvDev
PvDev

Reputation: 829

Selector function is not calling in iOS Swift

In first controller, Inside send action button adding notification post method

 let notificationhide = Notification.Name("hideLiveBtn")

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

And second camera controller calling notification add observer method inside view didload

 NotificationCenter.default.addObserver(self, selector: 
#selector(self.hideliveBtn(notification:)), name: notificationhide, object: nil)

Selector function :

@objc func hideliveBtn(notification : NSNotification){

    btnLiveSettings.isHidden = true

}

When keeping break points for add observer it observers but it does not calling hideliveBtn function.

Upvotes: 0

Views: 1103

Answers (2)

Mehul Sojitra
Mehul Sojitra

Reputation: 1181

I checked your code and its prefectly working.

class ViewController: UIViewController {

    let notificationhide = Notification.Name("hideLiveBtn")

    override func viewDidLoad() {
        super.viewDidLoad()

        NotificationCenter.default.addObserver(self, selector:#selector(self.hideliveBtn(notification:)), name: notificationhide, object: nil)
        NotificationCenter.default.post(name: notificationhide, object: nil)
    }

    @objc func hideliveBtn(notification : NSNotification){
        print("notification fired")
    }
}

Upvotes: 1

Khawar Islam
Khawar Islam

Reputation: 2914

Please implement NSNotification center by some another way.

Step 1:

extension NSNotification.Name {
    public static let hideLiveBtnNotificationKey = NSNotification.Name(rawValue: "hideLiveBtn")
}

Step 2

In viewdidLoad() of a controller

NotificationCenter.default.addObserver(self, selector: #selector(notificationReceived), name: .hideLiveBtnNotificationKey, object: nil)

Step 3

Selector function

@objc func hideliveBtn(notification : NSNotification){

    btnLiveSettings.isHidden = true

}

Step 4

override func viewWillDisappear(_ animated: Bool) {
        NotificationCenter.default.removeObserver("hideLiveBtnNotificationKey")
    }

Upvotes: 1

Related Questions