gpt4 book ai didi

ios - 消除 Objective C 中 Delegation 的重复代码

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:11:31 29 4
gpt4 key购买 nike

项目中的 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];
}

现在,回到这个问题:我不想在每节课上重复它(就像现在这样);我想以更清洁的方式进行,有没有可能的解决方案?

最佳答案

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

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

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com