|
|
#1 |
|
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"]; 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 |
|
|
|
0
|
|
|
#2 |
|
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 |
|
|
|
0
|
|
|
#3 |
|
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 |
|
|
|
0
|
|
|
#4 |
|
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; |
|
|
|
0
|
|
|
#5 |
|
Thanks for your help.
I have this code now: Code:
int solution = 8; NSString *theValue = [NSString stringWithFormat:@"%d", solution]; initializer element is not a constant. |
|
|
|
0
|
|
|
#6 |
|
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]; |
|
|
|
0
|
|
|
#7 |
|
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
|
|
|
|
0
|
|
|
#8 |
|
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.
|
|
|
|
0
|
|
|
#9 |
|
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
|
|
|
|
0
|
|
|
#10 |
|
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]];
}
)
|
|
|
|
0
|
|
|
#11 |
|
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]];
}
![]() I just want it to be "number is 1" |
|
|
|
0
|
|
|
#12 |
|
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]]];
}
Code:
- (IBAction)een:(id)sender {
int solution = 1;
[labelUitkomst setText:[NSString stringWithFormat:@"number is %d", solution]];
}
|
|
|
|
0
|
|
|
#13 |
|
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
|
|
|
|
0
|
|
|
#14 |
|
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. |
|
|
|
0
|
![]() |
|
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
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.






) so probably I did something stupid.

Linear Mode

