如何在协议委托方法中发送对象以获取通知和操作对象

时间:2013-03-15 15:03:50

标签: ios objective-c delegates protocols

@protocol VideoDelegate <NSObject>

 @optional
-(void)videoPlayBackDidFinish:(NSObject*)currencyInfo;
-(void)videoPlayBackDidStart;

我想发送一个带有videoPlayBackDidFinish的json对象,这样我就可以收到它完成的通知,我可以选择使用该对象的部分

例如:object.valueOfWhatever

1 个答案:

答案 0 :(得分:0)

将JSON转换为字典,然后将其作为userInfo发送到通知中:

NSDictionary *quickDict = [NSDictionary dictionaryWithObjectsAndKeys:object1, key1, nil];

NSNotification* notification = [NSNotification notificationWithName:kVideoDidFinishNotification object:self userInfo:quickDict];
[[NSNotificationCenter defaultCenter] postNotification:notification];

我不确定如何让videoPlayBackDidFinish成为消息的接收者而不在你的委托中做这样的事情:

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

然后在方法中找到字典:

-(void)videoPlayBackDidFinish: (NSNotification*)notification
{
    NSDictionary *jsonInfo = [notification userInfo];
}