3

好的,这是我的问题示例,我在前 3 个单元格的附件视图中创建了一个 UISwitch

theSwitch = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
    [cell addSubview:theSwitch];
    cell.accessoryView = theSwitch;

并在接下来的 3 个单元格中添加 2 个滑块

        theSlider =  [[[UISlider alloc] initWithFrame:CGRectMake(174,12,120,23)] autorelease];
        theSlider.maximumValue=99;
        theSlider.minimumValue=0;
        [cell addSubview:theSlider];
        cell.accessoryView = theSlider;

之后,我添加了开关和滑块的动作

[(UISwitch *)cell.accessoryView addTarget:self action:@selector(switchToggled:) forControlEvents:UIControlEventValueChanged];

[(UISlider *)cell.accessoryView addTarget:self action:@selector(sliderValueChange:) forControlEvents:UIControlEventValueChanged];

只要在单元格中添加开关就可以了

我想这也许是我的@selector(switchToggled:)@selector(sliderValueChange:)

问题。

如果我切换 UISwitch ,它不会崩溃

但如果我触摸任何滑块,它会崩溃并收到消息:“[UISlider isOn]: unrecognized selector sent to instance”

这是我的空虚

 - (void)switchToggled:(id)sender{
        UISwitch *theSwitch = (UISwitch *)sender;
        UITableViewCell *cell = (UITableViewCell *)theSwitch.superview;
        UITableView *tableView = (UITableView *)cell.superview;
        NSIndexPath *indexPath = [tableView indexPathForCell:cell];



        if(theSwitch.on) {
               ...
    } 
        else {
              ...
     }

    }

sliderValueChange一样的

- (void)sliderValueChange:(id)sender{
    UISlider *theSlider = (UISlider *)sender;
    UITableViewCell *cell = (UITableViewCell *)theSlider.superview;
    UITableView *tableView = (UITableView *)cell.superview;

            ...
}

有谁知道如何给两个控制器一个动作?

十分感谢!

4

1 回答 1

2

更新
评论后,这里是您应该使用的一般选择器。
注意:您只需要使用此选择器调用 addTarget 一次。

-(void)generalSelector:(id)sender{
    if ([sender isKindOfClass:[UISlider class]]){
        UISlider *slider = (UISlider *)sender;
        NSLog(@"Slider value %f",slider.value);
    }else{
        UISwitch *temp = (UISwitch *)sender;
        NSLog(@"Switch is %@",temp.on?@"ON":@"OFF");
    }   
}
于 2010-10-20T08:03:08.490 回答