Reputation: 6662
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:
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
Is there a simple solution for this? :)
Upvotes: 2
Views: 347
Reputation: 1213
Change your textFieldChanged action to 'Editing Changed' and put all of your calculation code in the textFieldChanged action method.
Upvotes: 0
Reputation: 25740
Instead of using the action "Value Changed", use "Editing Changed" for a text field.
Upvotes: 3
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