iPhone SDK : Add UISlider and UISwitch in a table view only crash when touch slider?

标签 iphone action cell uislider uiswitch

好的,这是我的问题 例如,我在前 3 个单元格的accessoryView 中创建了一个 UISwitch

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

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

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

之后,我添加开关和 slider 的操作

[(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 ,它不会崩溃

但是如果我触摸任何 slider ,它就会崩溃并收到消息:“[UISlider isOn]:无法识别的选择器已发送到实例”

这是我的空白

 - (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;

            ...
}

有人知道如何对两个 Controller 执行操作吗?

非常感谢!

最佳答案

更新
注释之后,这是您应该使用的通用选择器。
注意:使用此选择器您只需调用 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");
    }   
}

关于iPhone SDK : Add UISlider and UISwitch in a table view only crash when touch slider?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3975785/

相关文章:

c# - Unity3D游戏: deal with constants within one file

html - 相对表格单元格中的垂直对齐

iphone - 获取 UIWebView 层次结构中的上一页

iphone - cocoa 和 cocoa touch : is an object on the dictionary?

asp.net-mvc-3 - 返回 CSV 文件的 MVC 操作

c++ - Cocos2d-x c++ - 在继续之前等待 Action 完成

ios - 在 UIAlertController 中添加复选框

c# - excel 细胞着色

excel - 如何使用键盘在Excel单元格中下拉菜单?

iphone - 如何将焦点设置到 iPhone 中的下一个 uitextfield?