gpt4 book ai didi

objective-c 协议(protocol)作为变量(实例)

转载 作者:行者123 更新时间:2023-12-02 05:26:28 24 4
gpt4 key购买 nike

我是 objective-c 的新手,我还不清楚以下是可能的

  1. 是否可以实例化一个协议(protocol)?
  2. 如果可以实例化一个协议(protocol),我该如何实现所需的方法?

编辑:

这就是我使用和实现协议(protocol)的方式:

@protocol myProtocol <NSObject>

@required
- (void)doneAction:(NSDate *)dateSelected;
@end

@interface datePickerView : UIView

@property (strong, nonatomic) UIDatePicker *datePicker;
@property (strong, nonatomic) UIButton *doneButton;
@property (strong, nonatomic) NSObject<myProtocol> *datePickerProtocol;

- (id)initWithDetails:(UIDatePicker *)datePick doneBtn:(UIButton *)doneBtn;
- (void)show;
@end

这里是“datePickerView”的实现

@synthesize datePicker = _datePicker;
@synthesize doneButton = _doneButton;
@synthesize datePickerProtocol = _datePickerProtocol;

- (id)initWithDetails:(UIDatePicker *)datePick doneBtn:(UIButton *)doneBtn
{
_datePicker = datePick;
_doneButton = doneBtn;
[_doneButton addTarget:self action:@selector(doneButtonClicked) forControlEvents:UIControlEventTouchUpInside];

[self addSubview:_datePicker];
[self addSubview:_doneButton];
}

- (void)doneButtonClicked
{
/** Code to hide the view goes here **/
if (_datePickerProtocol != nil)
[_datePickerProtocol doneAction:_datePicker.date];
}

这是使用“datePickerView”的类

/** .h file **/
@interface myController : UITableViewController <myProtocol>
{
}

@property (strong, nonatomic) IBOutlet UIDatePicker *datePicker;
@property (strong, nonatomic) IBOutlet UIButton *datePickDone;
/** More definitions goes here **/

/** .m file **/
@synthesize datePicker = _datePicker;
@synthesize datePickDone = _datePickDone;
/** More code goes here **/

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *selectedCell = [self.tableView cellForRowAtIndexPath:indexPath];
if ((![selectedCell.reuseIdentifier isEqualToString:@"dateAppliedCellIdentifier"]) &&
(![selectedCell.reuseIdentifier isEqualToString:@"closingDateCellIdentifier"]))
return;

if (_datePicker.superview == nil)
{
datePickerView = [[datePickerView alloc] initWithDetails:_datePicker doneBtn:_datePickDone];

[self.view.window addSubview: datePickerView];
}
/**
THIS IS WHERE I WANT TO PASS AN INSTANCE OF THE PROTOCOL (BUT AS ADVISED,
IT IS NOT POSSIBLE) DEPENDING ON WHICH CELL WAS TAPPED. THE REASON I WANT
PASS AN INSTANCE OF THE PROTOCOL INSTEAD OF "SELF" IS BECAUSE I WANT
TO AVOID DOING MULTIPLE "IF" STATEMENTS INSIDE
"(void)doneAction:(NSDate *)dateSelected" METHOD. I WOULD LIKE TO DO THE IFs
HERE AND PASS THE CORRECT INSTANCE OF THE PROTOCOL DEPENDING ON WHICH CELL
WAS TAPPED. EACH INSTANCE OF THE PROTOCOLS HAVE DIFFERENT IMPLEMENTATIONS OF
"doneAction".
**/
[datePickerView setDatePickerProtocol:self];
[datePickerView show];
}

//implementation of the protocol method
- (void)doneAction:(NSDate *)dateSelected
{
//IF STATEMENTS GOES HERE DEPENDING ON WHICH CELL WAS TAPPED
NSLog(@"Done action is called!");
}

请查看我对代码的评论,在这一行上方 [datePickerView setDatePickerProtocol:self]。由于我无法实例化协议(protocol)并分配给变量,除了从 Controller 实现“myProtocol”并在“doneAction”方法中执行多个 IF 语句之外,是否有替代或更好的方法来实现我的目标?

根据答案,似乎我需要定义实现“myProtocol”的类并创建这些类的实例并传递给“datePickerView setDatePickerProtocol”。我的理解是否正确或这样做是否合理?

最佳答案

没有协议(protocol)不能从中创建实例,如果可以的话,它们只是类。协议(protocol)只是一种定义一组方法的方法,任何类都可以实现,这样你就不必使用继承来让两个不同的类具有相同的方法。对您正在共享的类进行子类化会打扰父类的接口(interface)和实现,有时在类之间共享接口(interface)而不共享任何实现是有意义的。

关于objective-c 协议(protocol)作为变量(实例),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13003162/

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