How to pass userInfo in NSNotification?

后端 未结 4 1015
甜味超标
甜味超标 2020-12-23 19:36

I am trying to send some data using NSNotification but get stuck. Here is my code:

// Posting Notification
NSDictionary *orientationData;
if(iFromInterfaceOr         


        
相关标签:
4条回答
  • 2020-12-23 19:58

    Your selector must have : to accept parameters.
    e.g.

    @selector(orientationChanged:)
    

    then in the method declaration it can accept the NSNotification parameter.

    0 讨论(0)
  • 2020-12-23 20:08

    You get an NSNotification object passed to your function. This includes the name, object and user info that you provided to the NSNotificationCenter.

    - (void)orientationChanged:(NSNotification *)notification
    {
        NSDictionary *dict = [notification userInfo];
    }
    
    0 讨论(0)
  • 2020-12-23 20:10

    In swift To get userinfo object

         let dict = notification.userInfo
         print(dict)
    
    0 讨论(0)
  • 2020-12-23 20:21

    You are posting the notification correctly. Please modify the Notification Observer like following.

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:)
     name:@"Abhinav" object:nil];
    
    - (void)orientationChanged:(NSNotification *)notification
    {
        NSDictionary *dict = [notification userInfo];
    }
    

    I hope, this solution will work for you..

    0 讨论(0)
提交回复
热议问题