Oscar Apeland
Oscar Apeland

Reputation: 6662

Connecting UITextField to update a label

I'm still working on my converter app, and I have once again stumbled across a problem.

While trying to make all edits in the text field to trigger the calculation code, not a single thing happens. This is what happens atm:

trying to enter numbers

The calculation will not trigger until I change something in the PickerWheel. Which I'm not blaming the code for, as thats how I first made it.

While trying to fix this, I got some help from a buddy, and I added following code to make it work.

First,

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

in the beggining of .m, and the following in .h (I know its two, and thats probably wrong, but i wanted to try both.)

-(IBAction)textFieldChanged:(UITextField *)textField;
-(void)textFieldChanged:(UITextField *)textField;

The calculation is as follows:

#pragma mark -
#pragma mark PickerView Delegate

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
[self updateConversionLabel];
}

- (void)updateConversionLabel
{
float convertFrom = [[_convertRates objectAtIndex:[picker selectedRowInComponent:0]] floatValue];

float convertTo = [[_convertRates objectAtIndex:[picker 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:[picker selectedRowInComponent:0]];
NSString *convertToName = [_convertFrom objectAtIndex:[picker selectedRowInComponent:1]];



NSString *resultString = [[NSString alloc]initWithFormat:
                          @" %.4f %@",result, convertToName];
resultLabel.text = resultString;

NSString *formelString = [[NSString alloc]initWithFormat:
                          @" %.4f %@=", convertValue, convertFromName];
formelLabel.text = formelString;


}

I figured the error could be in interface builder, so here is the connections connections inspector

Is there a simple solution for this? :)

Upvotes: 2

Views: 347

Answers (3)

Daniel Hakimi
Daniel Hakimi

Reputation: 1213

Change your textFieldChanged action to 'Editing Changed' and put all of your calculation code in the textFieldChanged action method.

Upvotes: 0

lnafziger
lnafziger

Reputation: 25740

Instead of using the action "Value Changed", use "Editing Changed" for a text field.

Upvotes: 3

Edwin Iskandar
Edwin Iskandar

Reputation: 4119

Make sure your view controller conforms to UITextFieldDelegate. Set the viewController as the delegate of your UITextField in interface builder. Then implement this delegate method in your viewController:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;  

That delegate method gets fired every time you make changes to the textfield so trigger your calculation in there.

Upvotes: 1

Related Questions