How can I use NSNotificationCenter?

Hello. I need send events in my app, but I don’t understand how to do it.

I define onNotifyRecieved method

@interface MainNavigationViewController : UINavigationController
- (void)onNotifyRecieved: (NSNotification*) notification;

and try subscribe

override fun init(): UINavigationController {
    val obj = super.init()

                .addObserverSelectorNameObject(this, SEL("onNotifyRecieved:"), DrawerViewController.NOTIFICATION_ID, null)

    return obj

fun onNotifyRecieved(notification: Notification) {
        val notifyInfo: NSDictionary<String, Int> = notification.userData as NSDictionary<String, Int>
        println("Notify: ${notifyInfo["id"]}")

but it’s crash. I think problem with SEL(“onNotifyRecieved:”)

How to use NSNotificationCenter or these is some alternative?

I wrote
fun onNotifyRecieved(notification: Notification)
but need
fun onNotifyRecieved(notification: NSNotification)
Sry :confused:


I tried this code and worked fine:

    fun onNotifyRecieved(notification: NSNotification) {
        val userInfo = notification.userInfo() as NSDictionary
        System.out.println("onNotifyRecieved: " + userInfo.objectForKey("message"))

and send:

val userInfo = NSMutableDictionary.alloc().init() as NSDictionary
        userInfo.put("message", "hello")
        NSNotificationCenter.defaultCenter().postNotificationNameObjectUserInfo("DetailViewController", this, userInfo)

Full sample (70.8 KB)

Best Regards,

1 Like

Yes. I wrote something like that. Thank you for answer