从NSNotification获取对象详细信息

时间:2010-10-02 02:49:09

标签: iphone

如何从NSNotification对象中获取对象?任何线索?

2 个答案:

答案 0 :(得分:3)

发布时,可以在NSDictionary中包装许多对象。

NSDictionary *userInfo=[NSDictionary withObjectsAndKeys:obj1,key1,obj2,key2,nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"NOTI_NAME" 
                                                    object:self
                                                  userInfo:userInfo];

在你的观察者中:

-(void)notiObserver:(NSNotification *)notification{

    NSDictionary *userInfo=[notification userInfo];
    OBJ1 *obj1=[userInfo objectForKey:key1];

}

答案 1 :(得分:2)

非常简单。使用NSNotification的对象方法。

- (void)myMethod:(NSNotification* notification) {
    // Example with a NSArray
    NSArray* myArray = (NSArray*)[notification object];
    // Do stuff
}
相关问题