2

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

@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...
}

此处声明的所有委托都包含相同的功能。甚至他们的定义也是如此。这些委托中的每一个都在各自的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];
}

现在,提出问题:我不想在每一堂课中都重复它(就像现在一样);我想以更清洁的方式制作,有没有可能的解决方案?

4

1 回答 1

3

在一个通用超类中实现委托方法,并将所有协议重构为一个通用 TapCallbackDelegate 协议

于 2013-02-14T11:21:20.113 回答