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;
@end

and try subscribe

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

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

    return obj
}

@Selector("onNotifyRecieved:")
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:

Hi!

I tried this code and worked fine:

@Selector("onNotifyRecieved:")
    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 project:NotificationSample.zip (70.8 KB)

Best Regards,
Roland

1 Like

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