Giving notification to another class with NSNotificationCenter

前端 未结 2 1388
礼貌的吻别
礼貌的吻别 2021-01-14 06:19

So my goal is to deliver a notification to another class with using NSNotificationCenter, I also want to pass object with the notification to the o

2条回答
  •  忘掉有多难
    2021-01-14 07:09

    You must first register a notification name

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startLocating:) name:@"ForceUpdateLocation" object:nil]; // don't forget the ":"
    

    And then post a notification with a dictionary of parameters

    [[NSNotificationCenter defaultCenter] postNotificationName:@"ForceUpdateLocation" object:self userInfo:[NSDictionary dictionaryWithObject:@"1,2,3,4,5" forKey:@"categories_ids"]]; 
    

    and the method will be

    - (void)startLocating:(NSNotification *)notification {
    
        NSDictionary *dict = [notification userInfo];
    }
    

提交回复
热议问题