2015-04-03 143 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