错误: - - [UIButton setValue:]:发送到实例的无法识别的选择器

时间:2013-02-20 14:39:12

标签: ios uitableview uibutton

我创造了 1)表格中的自定义单元格,单元格内有1个按钮和1个滑块。

           NSString *CellIdentifier = [NSString stringWithFormat:@"%d",indexPath.row];

        if (obj_TextCell == nil) {                
                        obj_TextCell = [[[TextCell alloc] initWithStyle:UITableViewCellStyleDefault
                                                       reuseIdentifier:CellIdentifier] autorelease];

           }
    UIButton* Btn_music=[[UIButton alloc] initWithFrame:CGRectMake(130,180, 50,50)];

                if (indexPath.row==play_tg)
                {
                     [Btn_music setImage:[UIImage imageNamed:@"pausemusic.png"] forState:UIControlStateNormal];

                }else
                {
                    [Btn_music setImage:[UIImage imageNamed:@"playmusic.png"] forState:UIControlStateNormal];
                }             

              [Btn_music addTarget:self action:@selector(playAudio:) forControlEvents:UIControlEventTouchUpInside];
              Btn_music.tag = indexPath.row;
              [obj_TextMusicCell.contentView addSubview:Btn_music];
              [background bringSubviewToFront:Btn_music];
 UISlider *progress = [[UISlider alloc] initWithFrame:CGRectMake(180,195,320,10)];
            [progress setBackgroundColor:[UIColor clearColor]];
            progress.minimumValue = 0.0;
            progress.maximumValue=1.0;
            progress.continuous = YES;
            progress.value = 0.0;
            progress.tag=indexPath.row+1;
            NSLog(@"tag for slider========>%d",progress.tag);
            //[progress setTag:indexPath.row];
            [progress addTarget:self action:@selector(sliderChanged:) forControlEvents:UIControlEventValueChanged];
            [obj_TextMusicCell.contentView addSubview:progress];

2)按钮的IBACTION事件我已经获取了单元格。并调用了滑块​​移动的方法。

-(IBAction)playAudio:(id)sender
{
    UIButton *Btn_music=sender;    

    cell_Access = (TextMusicCell *)[tbl_tellTale viewWithTag:0];
    NSIndexPath *indexPath = [tbl_tellTale indexPathForCell:cell_Access];
     NSLog(@"cellindex==>%d",indexPath.row); 
if ( timer==nil)
        {
          timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(SongProgress) userInfo:nil repeats:YES];
           [timer fire];
        }

}

3)滑块中的方法有: -

-(IBAction)SongProgress
{

   UISlider* progress = (UISlider *)[cell_Access viewWithTag:1];
}

4)它工作正常。但是当我滚动表格视图或某个时候应用程序崩溃并给出以下错误: -

-[UIButton setValue:]: unrecognized selector sent to instance 0x96f9660
2013-02-20 19:54:21.008 shc[3776:19d03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton setValue:]: unrecognized selector sent to instance 0x96f9660'
*** First throw call stack:
(0x3dba012 0x2777e7e 0x3e454bd 0x3da9bbc 0x3da994e 0x108d1b 0x21c0b90 0x3d79376 0x3d78e06 0x3d60a82 0x3d5ff44 0x3d5fe1b 0x33de7e3 0x33de668 0x16bf65c 0x2cb6 0x2be5 0x1)
libc++abi.dylib: terminate called throwing an exception

- [UIButton setValue:]:无法识别的选择器发送到实例

请帮帮我

2 个答案:

答案 0 :(得分:0)

您正在为UIBUTTON * btn_music以及UISlider *进度使用相同的标记值,因此UISlider *进度获取UIBUTTON实例 如果您对UIBUTTON使用1到200个标记值,则使用不同的标记值,然后对UISlider * progress使用201加值

答案 1 :(得分:-1)

您可能正在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath中执行第一部分,如果是这样,每次UIButtons需要呈现该单元格时,您都会创建新的UISlidersUITableView

您应该添加UIButtonUISlider的属性,并检查它们是否已初始化...

编辑:

好的,如果你在自定义UITableViewCell中定义了属性Btn_musicprogress,你应该合成它们,然后像​​这样初始化它们:

// If the Button is not yet initialized, then do it
if(obj_TextMusicCell.Btn_music == nil)
{
    // Here you should do the initialization code that only have to be done the first time
    obj_TextMusicCell.Btn_music=[[UIButton alloc] initWithFrame:CGRectMake(130,180, 50,50)];
    obj_TextMusicCell.Btn_music.tag = indexPath.row;
    [obj_TextMusicCell.contentView addSubview:Btn_music];
}

// Now it already exists, just update it...
if(indexPath.row==play_tg)
{
    [obj_TextMusicCell.Btn_music setImage:[UIImage imageNamed:@"pausemusic.png"] forState:UIControlStateNormal];
}
else
{
    [obj_TextMusicCell.Btn_music setImage:[UIImage imageNamed:@"playmusic.png"] forState:UIControlStateNormal];
}

UISlider采用相同的想法,你就明白了......