如何使用selector方法传递多个参数

时间:2014-07-01 12:56:39

标签: ios iphone objective-c ipad

我有一个以编程方式创建的滑块

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;

但我需要将questionID参数发送到sliderAction方法 这是sliderAction方法:

-(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]);
}

我不知道怎么用questionID来调用它

3 个答案:

答案 0 :(得分:1)

您无法使UIControlEvents选择器具有多个参数。

但你可以这样做,

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 :(得分:1)

我会在这里做这样的事情:

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.

- (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]);
}

答案 2 :(得分:0)

不幸的是,您可以将单个对象作为参数传递。 因此,您必须传递NSDictionary并使用特定键读取数据。