PDA

View Full Version : Set text of label to integer




RutgerB
Jul 14, 2008, 03:42 PM
Hi

I want to display the value of an integer in a label.

I have this code to display a string in a label:

[solution setText:@"Hello"];


So, solution is the name of the label.

I want to display the value of the integer variabel called 'solution' in the label.

I know it's a very beginners question, so I was wondering if there's a good place to find simple coding things like this. I tried the Xcode help but I always find a bunch of articles about everything except what I'm looking for :/


Thanks for helping me!
-Rutger



louden
Jul 14, 2008, 03:56 PM
You could put it in an NSNumber and just get the description.

In my opinion, there's so much moving pointers of data around from page to page, it makes sense to bite the bullet and use NSNumber instead of raw C scalar types for numbes... Handle them right, and things just work. You don't have to worry about losing them from page to page. Just clean up after yourself.

That said, I'm a newb to Objective C, and doing math with NSNumber types is a beeyotch...

RutgerB
Jul 14, 2008, 04:20 PM
Thanks for your reply!

Could you perhaps post a little code snippet of simply how to assign such a NSNumber?

I found this in Xcode help:

+ (NSNumber *)numberWithInteger:(NSInteger)value

But what can I do with this :/

grimjim
Jul 14, 2008, 04:32 PM
An easy way to do this would be to use an old-fashioned formatted string to convert your int to an NSString.

Try this (I'm calling the label that will display the value 'solutionLabel', and the integer 'solution', to try and make things clearer:

NSString *theValue = [NSString stringWithFormat:@"%d", solution];
solutionLabel.text = theValue;

It's a bit of a 'cheat', but it should work.

RutgerB
Jul 14, 2008, 04:48 PM
Thanks for your help.

I have this code now:
int solution = 8;
NSString *theValue = [NSString stringWithFormat:@"%d", solution];

This is just for assigning 8 to the string theValue. But it gives me this error:
initializer element is not a constant.

grimjim
Jul 14, 2008, 05:03 PM
That's strange. Where does XCode report the error?

The code you're using compiles OK for me.

By the way, if you really want to use NSNumber, you could try:

NSNumber *theSolution = [NSNumber numberWithInt: solution];
solutionLabel.text = [theSolution stringValue];

RutgerB
Jul 14, 2008, 05:07 PM
It reports the error at the second line of my code snippet. I've had this error once before.. maybe something's wrong somewhere else.

I'm new to developing for the iPhone (as you could have guessed :D) so probably I did something stupid.

I have experience with c# and ActionScript but seems like this is something a little more difficult. But it's a nice challenge for spending the summer so I'm very glad you guys are helping me :)

grimjim
Jul 14, 2008, 05:20 PM
Do you by any chance have this piece of code outside of the @implementation block? If you do, the compiler will consider that you are trying to initialise a global variable using another variable (ie, not a constant). That's a no-no, so it will give you this error. If you ensure that the code falls inside a method that is inside the @implementation block, then I believe that it *should* (note that I'm not saying *will* :)) compile correctly.

RutgerB
Jul 14, 2008, 05:45 PM
Yeah, it was but putting the code inside the @implementation block doesn't stop the error. This is my complete code of the class that does the calculations (I'm trying to make a calculator)

I have a label called 'uitkomst' and 6 buttons (een, twee, drie, vier, vijf and zes; the dutch words for 1 to 6). (I am planning on adding 7,8,9 and 0 :) ).



#import "RMBerekeningen.h"



@implementation RMBerekeningen

int solution = 8;
NSString *theValue = [NSString stringWithFormat:@"%d", solution];

- (IBAction)een:(id)sender {
[uitkomst setText:@"1"];
}

- (IBAction)twee:(id)sender {
[uitkomst setText:@"2"];
}
- (IBAction)drie:(id)sender {
[uitkomst setText:@"3"];
}
- (IBAction)vier:(id)sender {
[uitkomst setText:@"4"];
}

- (IBAction)vijf:(id)sender {
[uitkomst setText:@"5"];
}

- (IBAction)zes:(id)sender {
[uitkomst setText:@"6"];
}
@end


I'm just wondering: is it possibly that I need to change something in the .h file? The .h file looks like this:

#import <UIKit/UIKit.h>


@interface RMBerekeningen : NSObject {
IBOutlet id uitkomst;
}
- (IBAction)een:(id)sender;
- (IBAction)drie:(id)sender;
- (IBAction)twee:(id)sender;
- (IBAction)vier:(id)sender;
- (IBAction)vijf:(id)sender;
- (IBAction)zes:(id)sender;
@end

grimjim
Jul 14, 2008, 06:04 PM
The line that starts NSString *must* be inside a method. That is what is causing the error.

For test purposes, try adding the offending line to one of the methods that you already have, or maybe make a new method for, say the "=" key:

- (IBAction)evenaart:(id)sender {
[uitkomst setText:[NSString stringWithFormat:@"%d", solution]];
}

(Sorry if that's the wrong word for "equals" - my Dutch is a bit rusty...:))

RutgerB
Jul 15, 2008, 12:49 PM
Thanks, the erros stopped :).

I have this code now that is executed when you touch a button "een".

- (IBAction)een:(id)sender {
NSNumber *solution = [NSNumber numberWithInt:1 ];
[labelUitkomst setText:[NSString stringWithFormat:@"number is %d", solution]];
}

The result shown in labelUitkomst is: "number is 4245024". Wow :confused:
I just want it to be "number is 1"

grimjim
Jul 15, 2008, 03:21 PM
That's because you're using a mixture of the two suggestions I gave you. If you want to use an NSNumber, you can just use NSNumber's stringValue: method to return you an NSString representation of its value:

- (IBAction)een:(id)sender {
NSNumber *solution = [NSNumber numberWithInt:1 ];
[labelUitkomst setText:[NSString stringWithFormat:@"number is %@", [solution stringValue]]];
}

Otherwise, if you just declare solution to be an int rather than an NSNumber, it should just work:

- (IBAction)een:(id)sender {
int solution = 1;
[labelUitkomst setText:[NSString stringWithFormat:@"number is %d", solution]];
}

RutgerB
Jul 15, 2008, 03:35 PM
Thank you very much it works!

I'm now going to read some more on objective c, so I don't have to keep on asking dumb questions :rolleyes:

grimjim
Jul 15, 2008, 04:40 PM
Excellent! I'm glad you're up and running.
Objective-C is very straightforward, really, but you're bound to have a few problems as you learn it. Apple's documentation is generally very good, though, and they provide lots of examples, so it's not too difficult to figure things out once you get the idea of how things work.
If you're looking for a general book on programming using Objective-C, I recommend "Programming in Objective-C" by Stephen Kochan. It's not iPhone (or even Mac) specific, but it is a very clear introduction to the language. There's a new edition, covering Objective-C 2.0, coming out at the end of the year, if you're prepared to wait.