2015-04-03 148 views
0

我有一个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; 

我怎么可能使它工作?这是我的第一个协议,所以我不知道我该怎么做才对。

+0

您可以发布协议吗?看来你的协议声明 – 2015-04-03 21:44:22

+0

@CatalinaT有问题。当然,我已经更新了我的问题。 – rihe 2015-04-03 21:57:32

+0

您可以尝试在'DataProtocol.h'文件中声明枚举,因为这是它应该声明的地方,因为它连接到协议,而不是单元。 – 2015-04-06 20:50:50

回答

0

从您所描述的看来,您尚未将您的MyUserCellAction枚举的定义导入到您的协议中。如果你想使用枚举,我会将MyUserCellAction枚举的定义移动到协议中(无论如何,你的单元格正在导入协议,因此可以访问此定义)。我也会调用一些更具描述性的协议,比如CellActioning或ContactsTableViewCellDelegate。

另一方面,我会建议根本不通过枚举,而是有两种方法performSegueActionForUser:(PFUser *)userperformCheckActionForUser:(PFUser *)user或沿着这些线。

将枚举或布尔标志传递给方法调用以决定该方法应执行的操作是控制耦合的一个示例,如果遵循适当的软件工程原则,则该方法是不鼓励的。

http://en.wikipedia.org/wiki/Coupling_%28computer_programming%29