Send and receive messages through NSNotificationCenter in Objective-C?

后端 未结 6 1492
感动是毒
感动是毒 2020-11-21 22:40

I am attempting to send and receive messages through NSNotificationCenter in Objective-C. However, I haven\'t been able to find any examples on how to do this.

6条回答
  •  遇见更好的自我
    2020-11-21 23:02

    This one helped me:

    // Add an observer that will respond to loginComplete
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                                 selector:@selector(showMainMenu:) 
                                                     name:@"loginComplete" object:nil];
    
    
    // Post a notification to loginComplete
    [[NSNotificationCenter defaultCenter] postNotificationName:@"loginComplete" object:nil];
    
    
    // the function specified in the same class where we defined the addObserver
    - (void)showMainMenu:(NSNotification *)note {
        NSLog(@"Received Notification - Someone seems to have logged in"); 
    }
    

    Source: http://www.smipple.net/snippet/Sounden/Simple%20NSNotificationCenter%20example

提交回复
热议问题