项目中的 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/