Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • NSNotificationCenter

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 400
    Comment on it

    An NSNotificationCenter is used for broadcasting information within a program. It is like television broadcast where messages are broadcasted throughout the program. We can send the notification when some event occurs.

    For example, There is no much distinction between placing an order at a food cart and working with an NSNotification. You go to the food cart, give the details of your order at the counter and simply wait for your number to be called. More often than not, you don’t wait alone since there are others too who are also waiting for their number to be called. Once your order is ready, you hear the announcement being made for your number. The last thing is to find yourself a seat and begin eating.

    In the case of NSNotification, you keep a watch on your number. Your number is called by NSNotificationCenter once the object posting the notification is ready with your food.

    To implement NSNotificationCenter we have to take one ViewController, let's take the default one and add one button and one label on it. From button  push a segue to new ViewController having name SecondViewController. In SecondVC add one Textfield and a button. Embed navigation controller to FirstVc .


    We want if we write something in Textfield and click on a button so notification should be posted and in FirstVC, we will receive the notification. Once we received the notification in FirstVC then our data which we entered in SecondVC should display in the label of FirstVC.

    We give one notification name that works as a key for a particular notification at the time of PostNotification/PushNotification and we use the same key at the time of receiving data. Here addObserver is for receiving notification. In the below code we used "notificationKey" as a key for sending and receiving notification.

    Write this code in FirstVC

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                     name:@"notificationKey" object:nil]; //used to receive notification
    -(void)receivedNotification:(NSNotification*) notification
        NSString * name; //name of the notification
        //notification userinfo
        NSDictionary * info =notification.userInfo;
        [self.yourLabelOutlet setText:notification.userInfo];
        NSLog(@"Received Notification with name =%@",name);
        NSLog(@"Information =%@",info);

    Write Below code in SecondVC

    - (IBAction)buttonAction:(id)sender {
        NSString * noticationName =@"notificationKey";
        [[NSNotificationCenter defaultCenter] postNotificationName:noticationName
                                                            object:nil userInfo:yourTextFieldOutlet.text];


    It’s important  to remove observers before they’re deallocated so that your app won't get crashed . We can remove observer by using following code.

    -(void) dealloc {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
        [super dealloc];


 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: