Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jul 14, 2008, 03:42 PM   #1
RutgerB
macrumors newbie
 
Join Date: Jul 2008
Set text of label to integer

Hi

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

I have this code to display a string in a label:
Code:
[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
RutgerB is offline   0 Reply With Quote
Old Jul 14, 2008, 03:56 PM   #2
louden
macrumors 6502a
 
louden's Avatar
 
Join Date: Dec 2003
Location: Seattle
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...
__________________

8 Core 2.8 GHz Mac Pro
Core i7 11" MBA Ultimate
louden is offline   0 Reply With Quote
Old Jul 14, 2008, 04:20 PM   #3
RutgerB
Thread Starter
macrumors newbie
 
Join Date: Jul 2008
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:

Code:
+ (NSNumber *)numberWithInteger:(NSInteger)value
But what can I do with this :/
RutgerB is offline   0 Reply With Quote
Old Jul 14, 2008, 04:32 PM   #4
grimjim
macrumors member
 
Join Date: May 2003
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:

Code:
NSString *theValue = [NSString stringWithFormat:@"%d", solution];
solutionLabel.text = theValue;
It's a bit of a 'cheat', but it should work.
grimjim is offline   0 Reply With Quote
Old Jul 14, 2008, 04:48 PM   #5
RutgerB
Thread Starter
macrumors newbie
 
Join Date: Jul 2008
Thanks for your help.

I have this code now:
Code:
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.
RutgerB is offline   0 Reply With Quote
Old Jul 14, 2008, 05:03 PM   #6
grimjim
macrumors member
 
Join Date: May 2003
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:

Code:
NSNumber *theSolution = [NSNumber numberWithInt: solution];
solutionLabel.text = [theSolution stringValue];
grimjim is offline   0 Reply With Quote
Old Jul 14, 2008, 05:07 PM   #7
RutgerB
Thread Starter
macrumors newbie
 
Join Date: Jul 2008
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 ) 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
RutgerB is offline   0 Reply With Quote
Old Jul 14, 2008, 05:20 PM   #8
grimjim
macrumors member
 
Join Date: May 2003
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.
grimjim is offline   0 Reply With Quote
Old Jul 14, 2008, 05:45 PM   #9
RutgerB
Thread Starter
macrumors newbie
 
Join Date: Jul 2008
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 ).

Code:
#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:

Code:
#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
RutgerB is offline   0 Reply With Quote
Old Jul 14, 2008, 06:04 PM   #10
grimjim
macrumors member
 
Join Date: May 2003
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:

Code:
- (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...)
grimjim is offline   0 Reply With Quote
Old Jul 15, 2008, 12:49 PM   #11
RutgerB
Thread Starter
macrumors newbie
 
Join Date: Jul 2008
Thanks, the erros stopped .

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

Code:
- (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
I just want it to be "number is 1"
RutgerB is offline   0 Reply With Quote
Old Jul 15, 2008, 03:21 PM   #12
grimjim
macrumors member
 
Join Date: May 2003
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:

Code:
- (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:

Code:
- (IBAction)een:(id)sender {
	int solution = 1;
	[labelUitkomst setText:[NSString stringWithFormat:@"number is %d", solution]];
}
grimjim is offline   0 Reply With Quote
Old Jul 15, 2008, 03:35 PM   #13
RutgerB
Thread Starter
macrumors newbie
 
Join Date: Jul 2008
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
RutgerB is offline   0 Reply With Quote
Old Jul 15, 2008, 04:40 PM   #14
grimjim
macrumors member
 
Join Date: May 2003
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.
grimjim is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Set Text To label in Xcode ahan.tm Mac Programming 10 Jun 27, 2011 05:25 PM
iwork 08 pages: Setting part of document to landscape jtdolphins Mac Applications and Mac App Store 5 Nov 18, 2009 12:11 PM
Applescript Set Text of Label jaikob Mac Programming 0 Feb 12, 2009 06:24 PM
Placing texts on Table instead of label gryym iPhone/iPad Programming 3 Jul 22, 2008 08:28 PM
Setting text in a label to a variable upon button press Jeremy1026 iPhone/iPad Programming 1 Jul 18, 2008 09:39 AM


All times are GMT -5. The time now is 03:53 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

Privacy / DMCA contact / Affiliate and FTC Disclosure
Copyright 2002-2013, MacRumors.com, LLC