Display answer in UITextField?
Lets say I h开发者_如何学编程ave:
int a = textbox1;
int b = textbox2;
answer = a + b;
How do you get "answer" to display in textbox3? I'm sure this is a very simple question, but believe it or not, I have done a lot of researching and found nothing. If you could please give an example as "Text" and as "numbers with decimals" I would greatly appreciate it!! Thank you in advance!
Use the text
property of NSString
to get and set it's text. Use intValue
method of NSString
to convert the string to integer (assuming that the string contains a valid integer). And finally use stringWithFormat
to create a string which contains an integer.
Here is the one line code.
textBox3.text = [NSString stringWithFormat:@"%d", [textBox1.text intValue] + [textBox2.text intValue]];
a and b should probably be floats or doubles if you want decimal values. You can try something like this:
double a = [[textBox1 text] doubleValue];
double b = [[textBox2 text] doubleValue];
[textBox3 setText:[NSString stringWithFormat:@"%f", (a + b)]];
精彩评论