PDA

View Full Version : Help Adding NSUInteger to Label




mandude
Dec 14, 2009, 10:01 PM
he i need help adding an NSUInteger number to my label by +1 everytime the view loads.. so the first time you see the view load the label says "1" the next time it loads the label says "2" then "3" and "4" and so on. thanks



h.file:


@implementation....... {
NSUInteger levelNumber;
NSString *string;
UILabel *label;
}
@property (nonatomic, retain) IBOutlet UILabel *label;
@property (nonatomic, retain) NSString *string;
@end

m. file:

-(void)viewDidLoad {
levelNumber = levelNumber +1;
string = [NSString stringWithFormat:@"%d", levelNumber];
label.text = string;
}



kainjow
Dec 14, 2009, 10:02 PM
Well it depends how you're using the view controller. If you keep creating it and destroying it, the number will never increment. You could make it static, or you could save it in NSUserDefaults. Or if you're keeping the controller in memory, try overriding viewWillAppear instead of viewDidLoad.

mandude
Dec 14, 2009, 10:05 PM
ya i am actually continuously creating and destroying it lol how can i make it happen viewWillAppear causes a crash :P

kainjow
Dec 14, 2009, 10:07 PM
If you want the value saved for when the app quits, read and write it to NSUserDefaults. If you just want it to increment only while the app is active, simplest way is to move NSUInteger levelNumber; outside of the @interface and make it static.

mandude
Dec 14, 2009, 10:10 PM
I LOVE YOU thanks for helping dude putting the NSUInteger out of the @implementation worked great lol how do people know this stuff?

mandude
Dec 14, 2009, 10:13 PM
ok just a tiny drop more help :P so when i quit the view... go into a different view... and go back into my Label view.. the label adds the number to it. great. now when i quit into a totally different NIB... we'll call that "NIB X" how can i make it that when i go back into the label NIB from "NIB X" that label goes all the way back to 1

mandude
Dec 14, 2009, 10:18 PM
nevermind i got it XD

Luke Redpath
Dec 15, 2009, 03:43 AM
lol how do people know this stuff?

They learn how to read the documentation and learn the basics.