2012-02-06 70 views
-2

我想知道在目標C約NSNotification,任何一個可以告訴我一下右源NSNotification目標C

+0

[從這裏您可以開始,認真!!](http://www.google.com/search?q=NSNotification)和特別[this one](http://stackoverflow.com/q/1900352/593709) – 2012-02-06 10:21:04

+1

[doc](https://developer.apple.com/library/IOs/#documentation/Cocoa/Reference/Foundation/Classes/NSNotification_Class/Reference/Reference.html)? – 2012-02-06 10:21:57

回答

1

發送通知:

[[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotification" object:nil]; 

要註冊一個類來接收通知(通常在init方法):

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

- (void)myCallback:(NSNotification *)notification 
{ 
    ... do something 
} 

然後刪除觀察者在你的dealloc

[[NSNotificationCenter defaultCenter] removeObserver:self];