在不导入头文件的情况下检查和调用委托类的方法

时间:2013-06-21 13:33:20

标签: ios objective-c delegates

目前我正在开发一个iOS应用程序,我正在使用代理,我实现了类似的东西:

@protocol Hello <NSObject>

@required
- (void)update:(NSDictionary *)data;
@end


@interface NotificationHandler : NSObject
{
    id <Hello>delegate;
}
- (void)sendData;
@end

当某些特定通知发生时,它会调用sendData方法,并会调用update的{​​{1}}方法。

一切都很好。在字典中,我需要为一些特殊的委托传递特定的值。所以我输入的标题如下:

delegate

问题是,我需要在这里导入所有特殊类。无论如何都可以在不导入课程的情况下这样做吗?

1 个答案:

答案 0 :(得分:1)

我找到了一个使用NSClassFromString的解决方案,如:

- (void)sendData
{
   if([_delegate isKindOfClass:NSClassFromString(@"Special1")])
   {
      //special class 1
      NSDictionary *dict = //initialize with parameters and values
      [_delegate performSelector:@selector(update:) withObject:dict];
   }
   else if([_delegate isKindOfClass:NSClassFromString(@"Special2")])
   {
      //special class 2
      NSDictionary *dict = //initialize with parameters and values
      [_delegate performSelector:@selector(update:) withObject:dict];
   }
}