2013-02-20 53 views
-1

我打电话通知另一类如何从另一个类

//这是ClassA的

- (void)onDidFinishLaunchingNotification:(NSNotification*)notification 
{ 
    NSLOG(@"onDidFinishLaunchingNotification"); 
} 

从另一个类 //这是B级呼叫通知呼叫通知

[[NSNotificationCenter defaultCenter]addObserver:nil selector:@selector(onDidFinishLaunchingNotification:) name:nil object:nil]; 

回答

1

在A类中,添加自我作为通知的观察者名称

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(onDidFinishLaunchingNotification:) 
              name:YourOnDidFinishLaunchingNotificationName 
              object:nil]; 

和B类,使用-postNotificationName:object:张贴通知:

[[NSNotificationCenter defaultCenter] postNotificationName:YourOnDidFinishLaunchingNotificationName 
                object:nil]; 
+0

当我打电话时[[NSNotificationCenter defaultCenter] postNotificationName:YourOnDidFinishLaunchingNotificationName object:nil];我的应用程序崩溃了! – kiran 2013-02-24 00:47:18

+0

@kiran确保'selector'与消息名称相同:'onDidFinishLaunchingNotification:',不要忘记COLON。 – Kjuly 2013-02-24 02:39:11

1

B类你shuold的addObserver到B是这样的:

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(onDidFinishLaunchingNotification:) 
              name:YourOnDidFinishLaunchingNotificationName 
              object:nil]; 

我想你应该看看addObserver:selector:name:object:

这里是一个非常useful example使用NSNotification