监听所有的NSNotification

摘自 : http://www.helmsmansoft.com/index.php/archives/1552

NSNotificationCenter负责程序内的通知的监听和发送,而Darwin Notification Center负责程序间的通知的管理。
要想监听所有程序内的通知,只需要在addObserver方法里面的name属性设为nil即可:

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

查看每个通知的具体类别和内容,把他们打印出来:

- (void) trackNotifications: (NSNotification *) notification
{
   id nname = [notification name];
   id nobj = [notification object];
   id ndict = [notification userInfo];
 
   // notification name
   printf("%s\n", [nname cStringUsingEncoding:1]);
 
   // output accompanying data dictionary
   int i;
   id keys = [ndict allKeys];
   for (i = 1; i < [ndict count]; i++)
   {
      id key = [keys objectAtIndex:i];
      id object = [ndict objectForKey:key];
      NSLog(@"  %@ : %@", key, object);
   }
}
posted @ 2013-03-28 16:51  李伯波  阅读(154)  评论(0编辑  收藏  举报