0

i have a slider which i create programmatically

UISlider *mySlider = [[UISlider alloc] initWithFrame:CGRectMake(30,mainCategory.frame.size.height+subCategory.frame.size.height+questionLabel.frame.size.height +offsetQuestion,433,38)];
[mySlider addTarget:self action:@selector(sliderAction::) forControlEvents:UIControlEventValueChanged];
[mySlider setBackgroundColor:[UIColor clearColor]];
mySlider.minimumValue = 0;
mySlider.maximumValue = 100;

mySlider.continuous = YES;
mySlider.value = 0;

but i need to send questionID parameter to sliderAction method and here is the sliderAction method:

-(void)sliderAction:(int)questionID :(id)sender
{
    int discreteValue = roundl([(UISlider*)sender value]);

    UILabel *mylabel = (UILabel *)[self.scrlview.superview viewWithTag:questionID];
    mylabel.text=[NSString stringWithFormat:@"%d",discreteValue];
    NSLog(@"%d",[sender tag]);
}

i don't know how to call it with questionID

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
ercan
  • 825
  • 3
  • 16
  • 27
  • 2
    You can use NSArray or NSDictionary as parameter. See this link ------- http://stackoverflow.com/questions/8439052/ios-how-to-implement-a-performselector-with-multiple-arguments-and-with-afterd – Yogendra Jul 01 '14 at 12:59
  • 2
    The action event method (for example methods for handle button click, slider value change, etc.) needs to take just one parameter. Please explain more what you are trying to do (where do you get this questionId from?) and I'm sure someone helps you. – Greg Jul 01 '14 at 13:01
  • i want to generate dynamic form. and if question type for example slider i create it and add to scrollview. i also want to show value of slider with a uilabel. if i statically add label tag the above code works fine. But it should be dynamic and there could be many slider questions. so i need to pass questionID to know which label i write the value – ercan Jul 01 '14 at 13:19
  • if I'd need an integer value and I were you, I'd use the `tag` property of the `UIView` passing a value over. – holex Jul 01 '14 at 13:22
  • yes i tried that but the problem is while i create a slider control i also create a label control to set slider's value. so in for loop both of them take the same tag and in sliderAction method viewWithTag gives error – ercan Jul 01 '14 at 13:30

3 Answers3

1

You can't make UIControlEvents selector to have multiple parameters.

But you can do like so,

UISlider *mySlider = [[UISlider alloc] initWithFrame:CGRectMake(30,mainCategory.frame.size.height+subCategory.frame.size.height+questionLabel.frame.size.height +offsetQuestion,433,38)];

[mySlider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];

[mySlider setBackgroundColor:[UIColor clearColor]];
mySlider.minimumValue = 0;
mySlider.maximumValue = 100;

mySlider.continuous = YES;
mySlider.value = 0;





-(void)sliderAction:(id)sender
{

    [self slider:(UISlider *)sender withQuestionID:1 Or YourOwnValue here];

}


-(void)slider:(UISlider*)slider withQuestionID:(int)questionID
{

    int discreteValue = roundl([slider value]);

    UILabel *mylabel = (UILabel *)[self.scrlview.superview viewWithTag:questionID];
    mylabel.text=[NSString stringWithFormat:@"%d",discreteValue];
    NSLog(@"%d",[slider tag]);
}
1

I'd do such a thing here:

UISlider *mySlider = [[UISlider alloc] initWithFrame:CGRectMake(30,mainCategory.frame.size.height+subCategory.frame.size.height+questionLabel.frame.size.height +offsetQuestion,433,38)];
[mySlider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
[mySlider setBackgroundColor:[UIColor clearColor]];
mySlider.minimumValue = 0;
mySlider.maximumValue = 100;
mySlider.continuous = YES;
mySlider.value = 0;
mySlider.tag = // ... your question ID or whatever.

and:

- (void)sliderAction:(UISlider *)sender {

    NSInteger questionID = sender.tag;

    int discreteValue = roundl([(UISlider*)sender value]);
    UILabel *mylabel = (UILabel *)[self.scrlview.superview viewWithTag:questionID];
    mylabel.text = [NSString stringWithFormat:@"%d",discreteValue];
    NSLog(@"%d",[sender tag]);
}
holex
  • 23,961
  • 7
  • 62
  • 76
  • yes i tried that but the problem is while i create a slider control i also create a label control to set slider's value. so in for loop both of them take the same tag and in sliderAction method viewWithTag gives error – ercan Jul 01 '14 at 13:30
  • you may want to use an offset for the `UILabel` objects, like e.g. `UILabel` objects start from `1000` the `UISlider` objects start from `0`, and there won't be interference between the `tag` values when you call the `–viewWithTag:` method, you just need to increase the question ID by `1000` and those will be your labels' `tag` values, e.g. the `tag` `12` of `UISider` will match the `tag` `1012` of `UILabel`, etc... – holex Jul 01 '14 at 13:34
0

Unfortunately you can pass a single object as parameter. So you have to pass an NSDictionary and the read the data using specific keys.

Fry
  • 6,235
  • 8
  • 54
  • 93