gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 20:00:29 25 4
gpt4 key购买 nike

好的,这是我的问题例如,我在前 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com