我正在研究NSNotificationCenter。這裏是我的代碼通過伊娃槽NSNotificationCenter
Observer.m
//note init method is not complete here
-(id) init
{
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(reciveTestNotification:)
name:@"TestNotification" object:nil];
}
-(void)reciveTestNotification:(NSNotification *)notification
{
if([[notification name] isEqualToString:@"TestNotification"])
{
NSLog(@"Succesfuly received the test notification");
}
}
Osender.m
-(void)reciveTestNotification:(NSNotification *)notification
{
if([[notification name] isEqualToString:@"TestNotification"])
{
NSLog(@"Succesfuly received the test notification");
}
}
我認爲我已瞭解NSNotification是如何工作的,但如何通過NSNotification通過伊娃?
比方說Osender.h有這樣的代碼
Osender.h
@interface Osender : NSObject
{
IBOutlet UITextField *txt;
}
@property (nonatopic, copy) IBOutlet (UITextField *) *txt
如何通知reciveTestNotification和數據傳遞到它時,用戶類型或改變TXT的東西嗎?
我得到語義問題:未找到方法'-hasObjectForKey:'。我想這是因爲通知不響應該方法? – nedich 2012-01-30 12:33:37
固定。我的錯。你不必檢查'NSDictionary'中是否存在一個對象。如果沒有對象,它只返回'nil'。 – 2012-01-30 13:27:00