1

i have a class to read barcode, and when i read barcode i post a notification to NSNotificationCenter as below .

-(void)barcodeData:(NSString *)barcode type:(int)type {

    barcodeValue = barcode;

    [[NSNotificationCenter defaultCenter] postNotificationName:@"BarcodeRead" object:self];

}

then in a few view controller i add observer to get barcode value as like as .

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(BarcodeRead) name:@"BarcodeRead" object:nil];


-(void) BarcodeRead
{
    //
}

the question is when a notification is send to notification center, in all of the view which i add observer they get the notification and call the method BarcodeRead, but i want if the application is in view controller "A" just A get the notification and not all of them.

thanks for any help

ali
  • 1,023
  • 2
  • 14
  • 38
  • http://stackoverflow.com/a/2191802/64457 - worked for me to send one message to multiple receivers. If you want only one object to get the message - send different messages based on your receivers list. eg postNotificationName:@"BarcodeRead_ALL" vs postNotificationName:@"BarcodeRead_TARGET_1". You can add granularity to the posted message(s) to target specific objects – Paxic Feb 25 '12 at 22:30

2 Answers2

1

Then you should have the objects that should not receive the notification unregister themselves as observers once they go offscreen (an re-register when they come back onscreen, of course).

Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
1

I usually put the registration/unregistration code into the viewWillAppear / viewWillDisappear methods to ensure the notifications only show up in the controller while it's active.

Lakitu
  • 490
  • 4
  • 10