当滑块移动时,如何自动显示信息?

时间:2015-02-28 21:28:52

标签: ios objective-c uislider

我正在学习目标C而且我仍然坚持完成任务的最后一部分。在测试应用程序中,我正在比较两种不同饮料中的酒精含量。

目前,我可以移动滑块并按“计算”,它将显示饮料1中的酒精含量,相当于饮料2中的含量。(即3种啤酒含有与2杯葡萄酒一样多的酒精)。 / p>

我需要能够在移动滑块时显示数据,而不是按计算。我能够弄清楚如何在拉动滑块时显示数字,但无法找到有关自动更新数据的任何资源。任何帮助将不胜感激。

#import "BLCViewController.h"


@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *beerPercentTextField;

@property (weak, nonatomic) IBOutlet UISlider *beerCountSlider;

@property (weak, nonatomic) IBOutlet UILabel *resultLabel;

//@property (weak, nonatomic) IBOutlet UITextField *valueLabel;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.
}

- (IBAction)textFieldDidChange:(UITextField *)sender {
    NSString *enteredText = sender.text;
    float enteredNumber = [enteredText floatValue];

    if (enteredNumber == 0) {
        // The user typed 0, or something that's not a number, so clear the field
        sender.text = nil;
    }

}

- (IBAction)sliderValueDidChange:(UISlider *)sender {
    //NSLog(@"Slider value changed to %f", sender.value);
    //[self.beerPercentTextField resignFirstResponder];

    //When slider value changes, the code below is executed
  NSString *sliderValue =
    [NSString stringWithFormat:@"%f", self.beerCountSlider.value];
    self.beerPercentTextField.text = sliderValue;


    }


- (IBAction)buttonPressed:(UIButton *)sender {
    [self.beerPercentTextField resignFirstResponder];

    // first, calculate how much alcohol is in all those beers...

    int numberOfBeers = self.beerCountSlider.value;
    int ouncesInOneBeerGlass = 12;  //assume they are 12oz beer bottles

    float alcoholPercentageOfBeer = [self.beerPercentTextField.text floatValue] / 100;
    float ouncesOfAlcoholPerBeer = ouncesInOneBeerGlass * alcoholPercentageOfBeer;
    float ouncesOfAlcoholTotal = ouncesOfAlcoholPerBeer * numberOfBeers;

    // now, calculate the equivalent amount of wine...

    float ouncesInOneWineGlass = 5;  // wine glasses are usually 5oz
    float alcoholPercentageOfWine = 0.13;  // 13% is average

    float ouncesOfAlcoholPerWineGlass = ouncesInOneWineGlass * alcoholPercentageOfWine;
    float numberOfWineGlassesForEquivalentAlcoholAmount = ouncesOfAlcoholTotal / ouncesOfAlcoholPerWineGlass;

    // decide whether to use "beer"/"beers" and "glass"/"glasses"

    NSString *beerText;

    if (numberOfBeers == 1) {
        beerText = NSLocalizedString(@"beer", @"singular beer");
    } else {
        beerText = NSLocalizedString(@"beers", @"plural of beer");
    }

    NSString *wineText;

    if (numberOfWineGlassesForEquivalentAlcoholAmount == 1) {
        wineText = NSLocalizedString(@"glass", @"singular glass");
    } else {
        wineText = NSLocalizedString(@"glasses", @"plural of glass");
    }

    // generate the result text, and display it on the label

    NSString *resultText = [NSString stringWithFormat:NSLocalizedString(@"%d %@ contains as much alcohol as %.1f %@ of wine.", nil), numberOfBeers, beerText, numberOfWineGlassesForEquivalentAlcoholAmount, wineText];
    self.resultLabel.text = resultText;

}

- (IBAction)tapGestureDidFire:(UITapGestureRecognizer *)sender {
    [self.beerPercentTextField resignFirstResponder];

}

@end

1 个答案:

答案 0 :(得分:0)

最简单的方法是在滑块值更新时调用[self buttonPressed:nil]。它不是最干净的解决方案,但应该有效。