直接显示UISlider的范围值

时间:2014-12-26 21:06:03

标签: ios uislider

我有一个UISlider,具有UILabel显示范围值。但是当出现此视图时,没有默认值。我希望在出现此视图时显示默认值。

感谢您的回答。

- (void)ViewDidLoad {
    UISlider *firstSlider = [[UISlider alloc] init];
    firstSlider.frame = CGRectMake(30, firstView.bounds.size.height/2-15, firstView.bounds.size.width-60, 60);
    firstSlider.minimumTrackTintColor = HEXCOLOR(0xbbbbbb);
    firstSlider.maximumTrackTintColor = HEXCOLOR(0xdddddd);
    UIImage *sliderIMG = [UIImage imageNamed:@"slider.png"];
    [firstSlider setThumbImage:sliderIMG forState:UIControlStateNormal];
    firstSlider.minimumValue = 0;
    firstSlider.maximumValue = 100;
    firstSlider.value = 50;
    firstSlider.continuous = YES;
    [firstSlider addTarget:self action:@selector(getSliderValue:) forControlEvents:UIControlEventValueChanged];
    [firstView addSubview:firstSlider];

}

- (void)getSliderValue:(id) sender {
    UISlider *slider = (UISlider *)sender;
    int progressAsInt = (int)roundf(slider.value);
    NSString *firstResult = [NSString stringWithFormat:@"%d", progressAsInt];
    NSString *firstPercentResult = [firstResult stringByAppendingString:@" %"];
    _firstStat.text = firstPercentResult;
}

1 个答案:

答案 0 :(得分:1)

加载视图时不会调用您的操作方法。至少不是自动的。如果您不想在viewDidLoad方法中写出设置标签值,则可以调用滑块的操作方法。