iPhone SDK:在表格视图中添加 UISlider 和 UISwitch 仅在触摸滑块时崩溃?

发布于 2024-09-28 05:50:44 字数 1827 浏览 1 评论 0原文

好的,这是我的问题 敌人的例子,我在前3个单元格的accessoryView中创建一个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]:无法识别的选择器发送到实例”

这是我关于

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

            ...
}

有谁知道如何向两个控制器执行操作吗?

非常感谢!

OK,here is my question
foe example,I create a UISwitch in the first 3 cell's accessoryView

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

and add 2 slider in next 3 cells

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

after that , I add action to switch and slider

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

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

It's works if only add switch in cell

I think this is maybe my @selector(switchToggled:) and @selector(sliderValueChange:)

issue.

if I switch the UISwitch ,It won't crash

but if I touch any slider ,it crash and got message :"[UISlider isOn]: unrecognized selector sent to instance"

here is my void about

 - (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 {
              ...
     }

    }

the sliderValueChange all most the same as

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

            ...
}

Does anyone knows how to give an action to both controller ?

Great Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烂人 2024-10-05 05:50:45

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

UPDATE
After the comments, here is a general selector you should use.
Note: you will need to call addTarget only once using this selector.

-(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");
    }   
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文