How can I get UIPickerview to automatically update UILabel

I have a converter application and I want it to update the label whenever I enter numbers (which should be converted) in a text box. I want it to update automatically, so I don’t need to select the units on the wheel again for the update.

enter image description here

enter image description here

here is my code:

- (void)viewDidLoad { [super viewDidLoad]; { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. _convertFrom = @[@"MTPA", @"MMcf/day", @"Mill.Sm3/day", @"MMBTU", @"Boe/day"]; _convertRates = @[ @1.0f, @133.4246575f, @3.780821918f, @142465.7534f, @23780.8f]; _convertTo = @[@"MTPA", @"MMcf/day", @"Mill.Sm3/day", @"MMBTU", @"Boe/day"]; _convertRates = @[ @1.0f, @133.4246575f, @3.780821918f, @142465.7534f, @23780.8f]; } } 

shuold, am i doing something here?

 - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; 

}

 -(IBAction)textFieldReturn:(id)sender { [sender resignFirstResponder]; } -(IBAction)backgroundTouched:(id)sender { [inputText resignFirstResponder]; } #pragma mark - #pragma mark PickerView DataSource - (NSInteger)numberOfComponentsInPickerView: (UIPickerView *)pickerView { return 2; } - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component { if (component == 0) { return [_convertFrom count]; } return [_convertTo count]; } - (NSString *) pickerView: (UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { if (component == 0) { return [_convertFrom objectAtIndex:row]; } return [_convertTo objectAtIndex:row]; } #pragma mark - #pragma mark PickerView Delegate -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { float convertFrom = [[_convertRates objectAtIndex:[pickerView selectedRowInComponent:0]] floatValue]; float convertTo = [[_convertRates objectAtIndex:[pickerView selectedRowInComponent:1]] floatValue]; float input = [inputText.text floatValue]; float to = convertTo; float from = convertFrom; float convertValue = input; float relative = to / from; float result = relative * convertValue; NSString *convertFromName = [_convertFrom objectAtIndex:[pickerView selectedRowInComponent:0]]; NSString *convertToName = [_convertFrom objectAtIndex:[pickerView selectedRowInComponent:1]]; NSString *resultString = [[NSString alloc]initWithFormat: @" %.4f %@ = %.4f %@",convertValue, convertFromName, result, convertToName]; resultLabel.text = resultString; } 
+4
source share
1 answer

Your question has nothing to do with the idea of ​​the collector. You want to listen to the changes in the text box and update the conversion and label based on the last text entered in the text box.

  • Set the method that will be called in the text field when its value changes.
  • All code in the delegate method, which computes the transformation and updates the label, must be placed in another method that can be called from two places.
  • In a new method that responds to changes in the text box, call the method you just created that performs the conversion and updates the label.

So, at this point, your viewer delegate method and your text field change method call a method that gets the transform from the collector and text from the text box, then performs the transform and finally updates the label.

Edit: Details

Reorganize the delegation method of the current selection view, for example:

 -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { [self updateConversionLabel]; } - (void)updateConversionLabel { float convertFrom = [[_convertRates objectAtIndex:[pickerView selectedRowInComponent:0]] floatValue]; float convertTo = [[_convertRates objectAtIndex:[pickerView selectedRowInComponent:1]] floatValue]; float input = [inputText.text floatValue]; float to = convertTo; float from = convertFrom; float convertValue = input; float relative = to / from; float result = relative * convertValue; NSString *convertFromName = [_convertFrom objectAtIndex:[pickerView selectedRowInComponent:0]]; NSString *convertToName = [_convertFrom objectAtIndex:[pickerView selectedRowInComponent:1]]; NSString *resultString = [[NSString alloc]initWithFormat: @" %.4f %@ = %.4f %@",convertValue, convertFromName, result, convertToName]; resultLabel.text = resultString; } 

Then you need to create a change processing method in the text box:

 - (void)textFieldChanged:(UITextField *)textField { [self updateConversionLabel]; } 

Now in IB, connect the textFieldChanged: method to the event with the changed value for the inputText text field. I do not use IB, so I do not know the exact term for this.

+3
source

Source: https://habr.com/ru/post/1441107/


All Articles