本文介绍了iOS使用步进器控制滑块值和文本标签值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有了一个控制文本标签输出的滑块.

I already have a slider that controls the output of a text label.

-(IBAction)mySlider:(UISlider *)sender
{
    _myTextLabel.text = [NSString stringWithFormat:@"%i", (int) sender.value];
}

但是,我还希望步进器能够改变滑块的位置.因此,如果将滑块设置为50,并且文本标签显示为50,则我希望步进器增加到51、52、53等.有人知道执行此操作的代码吗?谢谢!

However, I also want a stepper to be able to changer the slider's position. So, if the slider has been set to 50, and the text label says 50, I want the stepper to increment to 51, 52, 53, etc. Does anyone know the code to do this? Thanks!

推荐答案

您将需要保留UISlider的引用.使用valueChanged事件为UIStepper创建处理程序.

You will need to keep an reference for the UISlider. Make a handler for UIStepper with valueChanged event.

步进器和滑块都需要进行一些设置

Some settings that need to be done for both stepper and slider

将步进器和滑块的最小值和最大值分别设置为0和100.将stepper的步长设置为1.将两个stepper ans滑块的默认值设置为相同.

Set minimum value and maximum value for both stepper and slider as 0 and 100 respectively. Set the step size of stepper as 1. Set the default value same for both stepper ans slider.

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.slider.minimumValue = 0;
    self.slider.maximumValue = 100;

    self.stepper.minimumValue = 0;
    self.stepper.maximumValue = 100;
    self.stepper.stepValue = 1;

    self.slider.value = 10;
    self.stepper.value = 10;

}

- (IBAction)stepperValueChanged:(UIStepper *)sender {

    [self.slider setValue:sender.value];

    self.myTextLabel.text = [@((int)sender.value) stringValue];

}

- (IBAction)sliderValueChanged:(UISlider *)sender {

    [self.stepper setValue:sender.value];

    self.myTextLabel.text = [@((int)sender.value) stringValue];
}

这篇关于iOS使用步进器控制滑块值和文本标签值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 04:13