ios - 具有多个实例的自定义委托(delegate)方法

标签 ios objective-c delegates multiple-instances

我创建了一个自定义类 NetworkManager。它有两个可选的委托(delegate)方法。它工作得很好,但是,问题是当有多个 NetworkManager 实例时如何执行委托(delegate)方法。这是我的代码片段;

NetworkManager.h

@protocol NetworkManagerDelegate <NSObject>

@optional
//call back for success message
-(void)handleSuccessMessageWithDictionary:(NSDictionary *) jsonDictionary;

//call back for fail message
-(void)handleFailureMessageWitDictionary:(NSDictionary *) jsonDictionary;

@end
@interface NetworkManager : NSObject

@property (nonatomic,assign) id<NetworkManagerDelegate> delegate;

@end

然后在我的 View Controller 中我有:

#pragma-mark NetowrkManager Delegate
-(void)handleFailureMessageWitDictionary:(NSDictionary *)jsonDictionary{

//failure


}

-(void)handleSuccessMessageWithDictionary:(NSDictionary *)jsonDictionary{

//success

}

这对于 NetworkManager 的一个实例来说工作得很好,但是如何区分两个实例呢?

例如,

#pragma-mark NetowrkManager Delegate
-(void)handleFailureMessageWitDictionary:(NSDictionary *)jsonDictionary{

//if its networkManagerA do
// method A
//else if its networkManagerB do
//method B

-(void)handleSuccessMessageWithDictionary:(NSDictionary *)jsonDictionary{

//if its networkManagerA do
// method A
//else if its networkManagerB do
//method B

}

非常感谢任何建议!

最佳答案

委托(delegate)对象的实例传递回委托(delegate)是正常的;请参阅 UITableViewDelegate 方法,例如:

- (BOOL)            tableView:(UITableView *)tableView
shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath;

这样一个委托(delegate)对象可以在多个实例上工作。

关于ios - 具有多个实例的自定义委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27783074/

相关文章:

iOS框架中的委托(delegate)方法

ios - 无法使用 echo 打印值

iPhone:UIWebview 在 safari 中打开超链接

ios - 为什么会出现此错误? - 使用未声明的标识符 'AppDelegate'

binding - Xamarin 绑定(bind)类

ios - 从父类获取属性

ios - 从 Google 语音识别中删除亵渎审查员

ios - 如何将 ScrollView 的大小设置为与 View 相同的大小

ios - 如何在 iPhone 中以编程方式保存带有时间戳的屏幕截图?

ios - 代码签名未显示配置文件 - Xcode 5.1。