Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

One Week New Programmer has Questions

I am trying to learn the basics to programming in Objective-C/Cocoa for the iPhone. I have made a simple interface with the Interface Builder in which there are Two Text Fields, 1 Label, and a Button. The purpose is to compute the sum of the first two fields into the label upon tapping the button; a very simple and purposeless calculator.

Here is the code from my header and corresponding method. I need help (1) converting the integer back to a string so that I can output it to the label (2) or I am wondering if my approach is overly complex, or missing some components. (3) Also if you could refer to me some good introductory tutorials besides those from apple.com,iPhonedevcentral, or cocoadevcentral I would GREATLY appreciate this!

=========HEADER===========
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@interface Calculator : UIView {
IBOutlet UITextField *field1; //First Text Field
IBOutlet UITextField *field2; //Second Text Field
IBOutlet UILabel *total; /*Label, which will be updated upon clicking the button to add field1&field2*/
}


@property (nonatomic, retain) UITextField *field1; //ability to declare int here?
@property (nonatomic, retain) UITextField *field2; //ability to declare int here?
@property (nonatomic, retain) UILabel *total;
- (IBAction)calculate;
@end
==========END HEADER======
==========METHOD=========
#import "Calculator.h"

@implementation Calculator
@synthesize field1;
@synthesize field2;
@synthesize total;

- (IBAction)calculate {

int intField1 = [field1.text integerValue];
int intField2 = [field2.text integerValue];
int intTotal = intField1 + intField2;
total.text = intTotal; // <<<<<THIS IS WHERE THE PROBLEM LIES

}
@end
========END METHOD===========

Thanks Again,

RK

Posted on Jul 27, 2008 3:33 PM

Reply
9 replies

Jul 27, 2008 6:07 PM in response to mrrish_

It's actually quite easy.

Most languages have a function called something like "printf" that lets you use a format string to insert various sorts of data into a template; it's a handy way to convert various types of data into strings. In Foundation (the framework Mac OS and iPhone OS share), we use the stringWithFormat: constructor of the NSString class:

total.text = [NSString stringWithFormat:@"%d", intTotal];

As in many languages, the format code %d indicates an integer. stringWithFormat: supports the same format codes C does, plus a %@ code for any Objective-C object.

You could also put the integer into an NSNumber (a wrapper object for various numeric types) and use its stringValue method, but this way is simpler.

Jul 27, 2008 6:18 PM in response to mrrish_

One technique that is commonly used to accomplished this is to use the methods made available to NSTextField by one of its parent classes, NSControl:

http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/ NSControlClass/Reference/Reference.html#//appleref/occ/cl/NSControl

This class provides a bunch of accessor methods (getters and setters) for the various data types. So you can use stringValue: and setStringValue: or intValue: and setIntValue: to get values from or send values to an NSTextField instance.

One Week New Programmer has Questions

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.