-(void) dealloc { // If you don't remove yourself as an observer, the Notification Center // will continue to try and send notification objects to the deallocated // object. [[NSNotificationCenter defaultCenter] removeObserver:self]; [super dealloc]; }
// Add this instance of TestClass as an observer of the TestNotification. // We tell the notification center to inform us of"TestNotification" // notifications using the receiveTestNotification: selector. By // specifying object:nil, we tell the notification center that we are not // interested in who posted the notification. If you provided an actual // object rather than nil, the notification center will only notify you // when the notification was posted by that particular object.
-(void) receiveTestNotification:(NSNotification *) notification { // [notification name] should always be @"TestNotification" // unless you use this method for observation of other notifications // as well.
if([[notification name] isEqualToString:@"TestNotification"])
NSLog (@"Successfully received the test notification!"); }
@end
…在另一个班级的其他地方…
1 2 3 4 5 6 7 8 9
-(void) someMethod {
// All instances of TestClass will be notified [[NSNotificationCenter defaultCenter]
postNotificationName:@"TestNotification"
object:self];
Just wondering where[NSNotificationCenter Default Enter]is meant to placed.在你的苹果里这是最好的吗?
@Fulvio:it depends,if you are receiving or posting notifications that potentially affect all parts of your application,put it in your appdelegate.如果你收到了只影响到一个类别的通知,就把它放在这些类别中。
Also,note that"the method specified by notificationselector must have one and only one argument(an instance of nsnotification)"。I initially assumed that you could provide a selector without an argument and forgo the nsnotification.
@Dreamlax truth,however it's worth nothing because this question is mostly searched by new ones devs who keep the notification listener alive longer than they need.Now with ARC You usually don't use dealloc and as a result some may think they don't have to release the listener.
DREAM,Perhaps you should consider also adding the definitive example for a disrubuted notification.
它也可能是Worth mentioning that the EDOCX1 plography 0-nible call in the dealloc-method is not permitted under ARC;the rest is all good.
It seems that all that observer idea doesn't cover all cases.当应用程序时,这项工作就无法进行。已经关闭了通知中心并在其上贴了标签。Observer method doesn't get called.
如果通知火灾和没有观察员会发生什么事?通知丢失?还是"拯救"了一些准备向新的观察家投降的人?
@Superuccio:If there are no observers,then nothing happens,the NSnotification is simply discarded.
@dreamlax:I am facing an issue with your code.在我简单的应用程序,有两个老古董。In the first VC,I have given a button,which when pressed,sends out the"testification"and performs a segure to the other VC,which is the observer in my case.I have declared the observer as self in this and rest,as is your code.The problem is the method'receivetestentification'doesn't get called at all…!甚至不是11!它不叫任何方式。and I have crossed check with your code,so there is hardly any difference.So,would you mind helping me or guide appropriate.
@Architkapor:check to make sure you are not posting the notification from a background thread.You can only update the user interface from the main thread.
@dreamlax:The notification is posted from the main thread;when the button is pressed an ibation is called where I post the notification and perform the segue thereafter in the same method.The Code where I am posting the notification is:[nsnotificationCenter defaultcenter]postnotificonname:@"testnotification"object:[observerviewcourtroller alloc]init]];
@dreamlax:I also tried this:[nsnotificationCenter defaultcenter]postnotificationname:@"testnotification"object:self];but my efforts went in vain.
To avoid the message"arc forbids explicit message send of ` dealloc'"just remove the invitation to its super classEDOCX1 universal 0,like:EDOCX1 universifical 2.Also add the observer to the main queue to avoid more problems.
// 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"); }