user1010563
user1010563

Reputation:

iOS Beginner: TextField doesn't show result of my calculation

I'm new to programming in general and currently trying to get a foot into Objective-C. Like to write my first app for the iPhone, should be a simple Golf app (for now). You enter your holes played and strokes needed on the green and the app tells you what your average is.

When it comes to displaying the result in an UITextField I get lost.

I thought the ollowing code would work but the result is not showing:

- (IBAction)calcNow(id)sender 
{

int holes = [[holesField text] intValue]; 

int strokes = [[strokesField text] intValue]; 

double result = strokes / holes; 

[averageField setText:result]; 
} 

Note:

The book I am reading tells me that a number, entered in an UITextField must be converted before I'm able to work with the value as a number.

I think this will do it:

[[holesField text] intValue]; 

Is this correct?

Thanks for your help!

Upvotes: 0

Views: 401

Answers (1)

El Developer
El Developer

Reputation: 3346

All you have to do is use:

[averageField setText:[NSString stringWithFormat:@"%f",result]];

Because setText: uses a NSString class as message.

Upvotes: 4

Related Questions