views:

59

answers:

1

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!

+1  A: 

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");
    }   
}
medopal
Yes I did ,just missing type here!
WebberLai
ok lets backup, when are you exactly calling the isOn on the "slider"? It's not showing in your code snippets.
medopal
I never call the isOn on slider ,only let it call when switch is toggled.
WebberLai
but somehow .......the slider call it maybe here is repeat addTarget:self ???
WebberLai
I just noticed you are calling addTarget twice in row. That's your problem, check my final answer.
medopal
Ok....but what should I place before cell at this code ??? [(UISwitch *)cell.accessoryView addTarget:self action:@selector(switchToggled:) forControlEvents:UIControlEventValueChanged];
WebberLai
ok it doesn't matter what I place this also work too : (UISwitch *)(UISlider *)cell.......
WebberLai
You better use `[(UIControl *)cell.acc....]`, since `UIControl` is the parent of both `UISlider` and `UISwitch`
medopal
thanks ,that's a brilliant answer !
WebberLai