我创建了一个自定义类 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/