代码之家  ›  专栏  ›  技术社区  ›  David Liu

正在收听所有CFNotifications?

  •  3
  • David Liu  · 技术社区  · 14 年前

    void callback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {  
        NSNotification* n = [NSNotification notificationWithName:name object:object userInfo:userInfo];
    
        objc_msgSend(gSelf, sel_getUid(@"note:"), n);
        return;  
    }  
    
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        gSelf = self;
        notifications = [[NSMutableArray alloc] initWithCapacity:10];
        self.title = @"Notification Log";
    
        CFNotificationCenterAddObserver(  
                                        CFNotificationCenterGetDarwinNotifyCenter(), //center  
                                        NULL, // observer  
                                        callback, // callback  
                                        NULL, // name  
                                        NULL, // object  
                                        CFNotificationSuspensionBehaviorHold  
                                        );   
    }
    
    -(void)note:(NSNotification *)notification{
    
        @synchronized(self){
            [notifications addObject:notification];
            [self.tableView reloadData];
        }
    }
    
    1 回复  |  直到 14 年前
        1
  •  4
  •   Nate    11 年前

    来自CFNotificationCenter.h:(查找 CFNotificationCenterGetDarwinNotifyCenter )或 the CFNotificationCenter reference

    CFNotificationCenterAddObserver():“name”参数不能为空(对于此中心)。