2

という名前のボタンを含むカスタム UITableViewCell サブクラスを作成しましたtestbtn。次に、そのボタンのイベントを別のクラスに割り当てようとしましたが、うまくいきません。どこが間違っているのか教えてください。

これが私のコードです:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"PatientCell";
    PatientCell *cell = (PatientCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
            NSArray *TopLevelOb = [[NSBundle mainBundle] loadNibNamed:@"PatientCell" owner:self options:nil];
            for (id currentlevelob in TopLevelOb) {
                if([currentlevelob isKindOfClass:[PatientCell class]]){
                    cell = (PatientCell *) currentlevelob;
                    cell.test.text = @"some text";
                    cell.HR.text =@"80";
                    cell.ABP.text =@"120/20";
                    cell.SOP2.text =@"95";
                    cell.RR.text=@"98";
                    [cell.testbtn addTarget:self action:@selector(openLiveData:)   
                       forControlEvents:UIControlEventTouchUpInside];// **this event is not fire to openLiveData method**
                    break;
                }
            }
        } else {
            NSArray *TopLevelOb = [[NSBundle mainBundle] loadNibNamed:@"PatientCell_iPad" owner:self options:nil];
            for (id currentlevelob in TopLevelOb) {
                if([currentlevelob isKindOfClass:[PatientCell class]]){
                    cell = (PatientCell *) currentlevelob;
                    cell.test.text = [nameArry objectAtIndex:indexPath.row];
                    cell.HR.text =@"80";
                    cell.ABP.text =@"120/20";
                    cell.SOP2.text =@"95";
                    cell.RR.text=@"98";
                    [cell.testbtn addTarget:self action:@selector(openLiveData:) forControlEvents:UIControlEventTouchUpInside]; 
                    break;
                }
            }     
        }
    }
    return cell;
}

-(IBAction)openLiveData:(id)sender{
     NSLog(@"hello ");
 }
4

3 に答える 3

1

ボタンが NIB の testBtn インスタンス変数に接続されているかどうかを確認します。

幸運を

T

于 2012-06-12T15:51:56.380 に答える
0

forControlEvents:UIControlStateNormalの代わりにおっしゃっていたように、iPhone 版では問題なく動作するはずですが、iPad 版では問題が発生するはずですforControlEvents:UIControlEventTouchUpInside

于 2012-06-12T07:00:33.300 に答える
0

コントロールイベントを指定するのを忘れたので、これを行います:

  [cell.testbtn addTarget:self action:@selector(openLiveData:) forControlEvents:UIControlEventTouchUpInside];

役立つことを願っています

于 2012-06-12T06:56:26.683 に答える