zoukankan      html  css  js  c++  java
  • 监听所有的NSNotification

    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);
    }
    }


    参考:http://blogs.oreilly.com/iphone/2008/09/uncovering-notifications-at-ru.html

    作者:

    Quite a while back, I posted about uncovering selectors at runtime. Detecting selectors allows you to reverse engineer the way Apple uses delegation calls to communicate between objects and clients. Delegation is a key way that objects communicate. Another way is intra-application notification.

    As with selectors, you can uncover notifications at runtime. Notification are electronic semaphores that pass between objects within a program.

    Notifications allow objects to broadcast information. Observers, objects that subscribe to those broadcasts, can respond to those notifications as needed by checking for new data, queuing a song, or any other state update process you can imagine.

    Apple's media playback classes offer a particularly good example of this. They broadcast when songs start playing, when they stop, when the track selection changes, and so forth.

    Unlike inter-application notifications, which use Darwin Notification Centers, intra-application notifications use the standard NSNotificationCenter class. Be aware that the iPhone generally does not use or properly implement its child class, NSDistributedNotificationCenter.

    Notification Centers can broadcast both a message and provide context for that message in a paired User Data -- typically a dictionary or array and an Object -- usually the object that broadcast the notification in the first place.

    In addition to providing user data, in-app Notification Centers differ from BSD/Darwin Notifications by allowing you to subscribe with wildcards. You don't need to know the name in advance of the notification you want to listen to. So to subscribe an object, all you need to do is pass a call like this:

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

    This registers your object (self) as an observer for all notifications that pass through your program. Here, the selector trackNotifications: is called with the notification whenever one is detected.

    To eavesdrop on those notifications, add the following method. This method extracts the notification name, object and user information from the notification itself.

    - (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);
       }
    }
  • 相关阅读:
    乐字节Java编程语言发展,面向对象和类
    乐字节Java编程之方法、调用、重载、递归
    乐字节Java循环:循环控制和嵌套循环
    乐字节Java反射之四:反射相关操作
    乐字节Java反射之三:方法、数组、类加载器和类的生命周期
    乐字节Java反射之二:实例化对象、接口与父类、修饰符和属性
    乐字节Java反射之一:反射概念与获取反射源头class
    Java变量与数据类型之三:数据类型与转义字符
    数论 N是完全平方数 充分必要条件 N有奇数个约数
    动态规划专题 01背包问题详解 HDU 2546 饭卡
  • 原文地址:https://www.cnblogs.com/cklxmu/p/2400216.html
Copyright © 2011-2022 走看看