我知道有一个关于此的问题,但我不清楚。我想向 UIAlertView 添加一个 UISlider。在 UIAlertView 中,我还添加了一个 UILabel 来显示 UISlider 的值。到目前为止我所做的是:
UIAlertView *alert= [[UIAlertView alloc]
initWithTitle: @"Set delay"
message: @" "
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
UISlider *mySlider=[[UISlider alloc] initWithFrame:CGRectMake(20, 50, 200, 20)];
mySlider.maximumValue=10.00;
mySlider.minimumValue=1.00;
UILabel *myLabel=[[UILabel alloc] initWithFrame:CGRectMake(220, 50, 50, 40)];
int x=.........;//x should get value of mySlider
[myLabel setText:[NSString stringWithFormat:@"%d",x]];
[alert addSubview:myLabel];
[alert addSubview:mySlider];
[alert show];
[alert release];
所以我希望变量 x
获取 mySlider
的值并且 myLabel
将显示 x
的值.我被困在这一点上。非常感谢任何建议。
最佳答案
这里是你需要做的:
- 使
UILabel
成为全局的,这意味着在头文件中声明它(这是为了轻松到达标签)。 然后使用
ValueChanged
事件将目标添加到 slider 。[mySlider addTarget:self action:@selector(sliderHandler:) forControlEvents:UIControlEventValueChanged];
接下来,实现
sliderHandler
方法:- (void) sliderHandler: (UISlider *)sender { int x = sender.value; [myLabel setText:[NSString stringWithFormat:@"value = %d",x]]; }
这对你有用。
关于UIAlertView 中的 iOS UISlider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11698217/