Question

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!

Was it helpful?

Solution

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");
    }   
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top