The text fields are just views that display a string.
You can access the string that they contain via the text
property:
NSString *text1 = _text1.text;
Now you have the string that is displayed in the textview ( "34" for example)
You can use the NSString
method intValue
to turn this string into an integer:
int text1Value = [text1 intValue];
You can now compare the value of this to another integer:
if (text1Value > 42) {
_textLargest.text = [NSString stringWithFormat:"%d", textValue1];
}
Main points:
- Text fields are just views; you need to work with the data they are displaying rather than treat them as objects
- You need to use the correct type for your comparison. The text field contains text but you need to convert it to a number to make it meaningful
- You need to convert the number back into a string if you want it to appear in the text field's text property
[NSString stringWithFormat:"%d", textValue1]