Help Adding NSUInteger to Label

Discussion in 'iPhone/iPad Programming' started by mandude, Dec 14, 2009.

  1. macrumors member

    Joined:
    Nov 19, 2009
    #1
    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;
    }
    
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    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.
     
  3. thread starter macrumors member

    Joined:
    Nov 19, 2009
    #3
    ya i am actually continuously creating and destroying it lol how can i make it happen viewWillAppear causes a crash :p
     
  4. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    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.
     
  5. thread starter macrumors member

    Joined:
    Nov 19, 2009
    #5
    I LOVE YOU thanks for helping dude putting the NSUInteger out of the @implementation worked great lol how do people know this stuff?
     
  6. thread starter macrumors member

    Joined:
    Nov 19, 2009
    #6
    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
     
  7. thread starter macrumors member

    Joined:
    Nov 19, 2009
    #7
    nevermind i got it XD
     
  8. macrumors 6502a

    Joined:
    Nov 9, 2007
    #8
    They learn how to read the documentation and learn the basics.
     

Share This Page