IPhone SDK: add UISlider and UISwitch in table view mode only when you click the slider?

OK, here is my question. For example, I create UISwitch in the first 3-seater accessory.

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

and add 2 sliders in the 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 an 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 works if you only add a switch to the cell

I think it's possible mine @selector(switchToggled:)and@selector(sliderValueChange:)

Question.

if I switch UISwitch it will not work

but if I touch any slider, it crashes and receives a message: "[UISlider isOn]: unrecognized selector sent to the instance"

here is my emptiness 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 sliderValueChangesame as

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

            ...
}

Does anyone know how to give action to both controllers?

Many thanks!

+3
1

UPDATE
, , .
: 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");
    }   
}
+2

Source: https://habr.com/ru/post/1770473/


All Articles