我有一個UITableViewCell
包含一個按鈕,當用戶點擊按鈕或選擇一個單元格時,我需要用兩個按鈕來呈現一個操作表。這兩個按鈕都會觸發一個循環,並且我將數據(來自單元格)傳遞給prepareForSegue:
方法。它工作正常,但我想用協議來做,但不幸的是我無法正確實施它。無法實現協議
這是我要做的事現在:
@property (nonatomic, strong) PFUser *userObj;
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ContactsTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell"];
PFUser *user = [self.dataSourceArray objectAtIndex:indexPath.row];
cell.usernm.text = user.username;
cell.userId.text = user.objectId;
cell.cellButton.tag = indexPath.row;
[cell.cellButton addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (void)buttonTapped:(id)sender {
UIButton *button = (UIButton *)sender;
self.userObj = [self.dataSourceArray objectAtIndex:button.tag];
[self setupActionSheet];
}
- (void) setupActionSheet {
AHKActionSheet *actionSheet = [[AHKActionSheet alloc] initWithTitle:NSLocalizedString(nil, nil)];
[actionSheet addButtonWithTitle:NSLocalizedString(@"Button 1", nil)
image:[UIImage imageNamed:@"1"]
type:AHKActionSheetButtonTypeDefault
handler:^(AHKActionSheet *as) {
[Utilz checkUser:self.userObj.objectId with:self.userObj.username];
}];
[actionSheet addButtonWithTitle:NSLocalizedString(@"Button 2", nil)
image:[UIImage imageNamed:@"2"]
type:AHKActionSheetButtonTypeDefault
handler:^(AHKActionSheet *as) {
[self performSegueWithIdentifier:@"presentNextView" sender:self];
}];
[actionSheet show];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"presentNextView"]) {
if ([[segue destinationViewController] isKindOfClass:[ViewControllerTwo class]]) {
ViewControllerTwo *dataToPass = [segue destinationViewController];
dataToPass.usrStr = self.usrObj.username;
dataToPass.objStr = self.usrObj.objectId;
}
}
}
這裏是我嘗試用協議來做到這一點:
首先,我創建了一個叫做協議DataPassProtocol.h
。
然後,我添加一個ENUM到ContactsTableViewCell.h
和進口DataPassProtocol.h
typedef NS_ENUM(NSInteger, MyUserCellAction) { MyUserCellActionCheckUser, MyUserCellActionPerformSegue };
我還添加這些變量到ContactsTableViewCell.h
@property (weak) id <DataPassProtocol> delegate;
@property (nonatomic, strong) PFUser *user;
- (IBAction)buttonTapped:(id)sender;
在ContactsTableViewCell.m
我添加這些方法
- (void)setUser:(PFUser *)user {
_user = user;
// setup UI
self.usernmLabel.text = _user.username;
self.userIdLabel.text = _user.objectId;
}
- (IBAction)buttonTapped:(id)sender {
// cell handles its button being tapped
NSLog(@"Button tapped");
if ([self.delegate respondsToSelector:@selector(myUser:didPerformAction:)]) {
[self.delegate myUser:self.user didPerformAction:MyUserCellActionCheckUser];
}
if ([self.delegate respondsToSelector:@selector(myUser:didPerformAction:)]) {
[self.delegate myUser:self.user didPerformAction:MyUserCellActionPerformSegue];
}
}
這裏我在第二條if語句時出錯。
Implicit conversion of 'NSInteger' (aka 'int') to 'id' is disallowed with ARC
我不知道爲什麼會發生這種情況。
另一個錯誤:
an Expected a type in this method implementation of the protocol file.
- (void)myUser:(PFUser *)user didPerformAction:(MyUserCellAction)action;
此外,在MyDelegateViewController
我得到這樣的警告:
Conflicting parameter types in implementation of 'myUser:didPerformAction:': '__strong id' vs 'MyUserCellAction' (aka 'enum MyUserCellAction')
而且我把這個在我的VC在我的表視圖。
- (void)myUser:(PFUser *)user didPerformAction:(MyUserCellAction)action {
if (action == MyUserCellActionCheckUser) {
// do what we want
} else if (action == MyUserCellActionPerformSegue) {
// do the other thing we want
}
}
,這裏是協議實現:
@protocol DataPassProtocol <NSObject>
@optional
- (void)myUser:(PFUser *)user didPerformAction:(MyUserCellAction)action;
我怎麼可能使它工作?這是我的第一個協議,所以我不知道我該怎麼做纔對。
您可以發佈協議嗎?看來你的協議聲明 – 2015-04-03 21:44:22
@CatalinaT有問題。當然,我已經更新了我的問題。 – rihe 2015-04-03 21:57:32
您可以嘗試在'DataProtocol.h'文件中聲明枚舉,因爲這是它應該聲明的地方,因爲它連接到協議,而不是單元。 – 2015-04-06 20:50:50