我是objective-c的新手,我仍然不清楚以下是可能的
- 是否可以实例化协议?
- 如果可以实例化一个协议,我该如何实现所需的方法?
编辑:
这就是我使用和实现协议的方式:
@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]。由于我无法实例化协议并分配给变量,除了从控制器实现“myProtocol”并在“doneAction”方法中执行多个 IF 语句之外,是否有替代或更好的方法来实现我的目标?
根据答案,我似乎需要定义实现“myProtocol”的类并创建这些类的实例并传递给“datePickerView setDatePickerProtocol”。我的理解正确还是这样做合理?