I am writing an application that takes a users input (from a UITextView) and then compares the input to a pre-defined value.
All inputs from the textfields are required to be numbers, so I use this code to convert the NSString into an int value when a button is pressed.
(age is the text view)
NSString *ageString = age.text;
int ageInt = [ageString intValue];
I then use this code to compare the text to a predefined list of numbers
if (ageInt > 12) {
label1.textColor = [UIColor greenColor];}
else {
label1.textColor = [UIColor redColor];
}
When I run the application everything works and I get no errors, however, when I enter a value into the “age” textView that is larger than the pre-defined value (lets say 13) it still turns label 1 red.
So the summary of this problem is that when the value is being compared, it doesn’t compare correctly. I’m curious if anyone knows why that is? Perhaps I am using the wrong classes?
NSLogageInt:right after:
That should show the problem, that
ageIntis not really > 12 and why.If the
ageIntis > 13 then either the color setting code is not being called or the color is being reset after the call.Add some breakpoints in Xcode and insure the correct color setter being called–and the value of
ageIntat that point.