Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mandude

macrumors member
Original poster
Nov 19, 2009
64
0
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
Code:
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;
}
 
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.
 
ya i am actually continuously creating and destroying it lol how can i make it happen viewWillAppear causes a crash :p
 
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.
 
I LOVE YOU thanks for helping dude putting the NSUInteger out of the @implementation worked great lol how do people know this stuff?
 
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.