ios - 消除 Objective C 中委托(delegate)的重复代码

标签 ios objective-c oop protocols delegation

项目中的 35 个头文件之一(由其他开发人员交给我;所有这些文件都包含相同的委托(delegate)声明)

@interface ActivityDetailsCN : UIViewController <NSXMLParserDelegate,     
AccountStatusDelegate, AccountTypeDelegate, DirectionDelegate, RecipientDelegate,
PriorityDelegate, DurationDelegate, CurrencyDelegate, OppTypeDelegate,
OppCategoryDelegate, DatePickerDelegate, SalutationDelegate, DepartmentDelegate, 
LeadTypeDelegate, OwnershipDelegate, MailingDelegate, SourceDelegate, 
StateDelegate, CommentsDelegate, CityDelegate, ZipCodeDelegate> 
{
    //Declaration of iVars goes here...
}

此处声明的所有委托(delegate)都包含相同的功能。甚至他们的定义也是如此。 每个委托(delegate)都在各自的 ViewController 头文件之前声明,如下所示:

@protocol AccountStatusDelegate <NSObject>
  - (void)cancelTapped;
  - (void)doneTapped;
  - (void)selectTapped:(NSString *)string;
@end

@interface AccountStatusVC : UIViewController <NSXMLParserDelegate> {
}
@property (unsafe_unretained) id <AccountStatusDelegate> delegate;

cancelTapped 的实现:

- (void)cancelTapped {
    [objPopOver dismissPopoverAnimated:YES];
}

cancelTapped 的实现:

- (void)doneTapped {
    [tblView reloadData];
    [objPopOver dismissPopoverAnimated:YES];
}

cancelTapped 的实现:

- (void)selectTapped:(NSString *)string
{
    if ([string isEqualToString:@"US"])
        isTextField = FALSE;
    else if([string isEqualToString:@"Other"]) {
        appDelegate.strCountry = @"";
        isTextField = TRUE;
    }
    [tblView reloadData];
    [objPopOver dismissPopoverAnimated:YES];
}

现在,回到这个问题:我不想在每堂课中重复它(就像现在一样);我想以更清洁的方式进行处理,有什么可能的解决方案吗?

最佳答案

在公共(public)父类(super class)中实现委托(delegate)方法,并将所有协议(protocol)重构为一个公共(public) TapCallbackDelegate 协议(protocol)

关于ios - 消除 Objective C 中委托(delegate)的重复代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14873595/

相关文章:

iOS 动画两个 View ,彼此堆叠

iOS:仅更改少数弹出 View 的方向

ios - 使图像出现在屏幕上?

iphone - 在iphone中创建递归后台处理程序是否合法?(应用程序进入后台时处理)

javascript - Titanium 项目的正确 CommonJS 结构是什么?

ios - AFNetworking - 数据已加载,但出现错误 504

iphone - 在 UITextfield 中使用 NSNumberFormatter 进行实时格式化

C++对象存储在栈或堆中

c# - 如何将 ListItemCollection (dropdownlist.items) 转换为字典<string,string>?

javascript - 强制 __proto__ 平等