Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 10-10-2009, 03:23 PM   #1 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 140
Default Help with saving score UIlabel

I have two labels, one that shows current level another that shows high level. The current level label is updated each time you beat the computer.

current_level_value++;

For the high level lable I want to do a check to see if the current level labels value is larger than the high level label value, and if so save and update the high level label value. Anb also do this when the user clickes the home button.

ViewController.h

Code:
@interface ViewController : UIViewController {
	
	
        IBOutlet UILabel *current_level;
	IBOutlet UILabel *high_level;

	NSInteger current_level_value;
	NSInteger high_level_value;
}

@property(nonatomic,retain) IBOutlet UILabel *current_level;
@property(nonatomic,retain) IBOutlet UILabel *high_level;

@end
I have this code in my ViewController.m

Code:
-(void)reset:(BOOL) newGame {

if(computer_score == kScoreToWin) {

// computer wins do stuff
			
		} 
		else {
//you win
			current_level_value++;	
			
		}
		
	} 

	current_level.text = [NSString stringWithFormat:@"%d",current_level_value];
}




- (void)viewDidLoad {

current_level_value = 0;

	high_level.text = [NSString stringWithFormat:@"%d",high_level_value];

	//CHECK THEN LOAD HIGH LEVEL LOADER
			if(current_level > high_level) {
				high_level.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"high_score"];
			}

	//SAVE HIGH LEVEL
	
	[[NSUserDefaults standardUserDefaults] setInteger:high_level_value forKey:@"high_score"];
	[[NSUserDefaults standardUserDefaults] synchronize];
		
[super viewDidLoad];
}
I'm not getting any errors or crashes, the current level label value is updating just fine, but when I quit and reopen the app the high level label value is still at zero.

Any thoughts?
Thanks

Quote:
Originally Posted by smasher View Post
It's probably better to start a new thread if you have a new question. Your "if" statement is bad - current_level and high_level are pointers (memory addresses,) so comparing them will not give you what you want. You should be comparing something else instead, like [current_level.text intValue] > [high_level.text intValue] .

Also it looks like you're doing the loading and saving in
viewDidLoad? Don't you need to save when the game is over, not when it's starting?
Thanks Smasher,

I was able to do the check, current level to high level, and it updates high level after the level ends but only durring your game session.

I used this code to do that.

Code:
		//COMPARE CURRENT LEVEL WITH HIGH LEVEL
		if([current_level.text intValue] > [high_level.text intValue]){
			
			high_level.text = current_level.text;

		}
But i cant seem to get the high level to save and load in when you quit and restart.

I tried to place this in the view did load, and nothing.

Code:
 //SAVE HIGH LEVEL
//[[NSUserDefaults standardUserDefaults] setInteger:high_level_value forKey:@"high_score"];
			//[[NSUserDefaults standardUserDefaults] synchronize];
And i cant seem to add it to my AppDelegate.m in the code below, because "high_level_value" isnt being implemented ther, its in my ViewController.m
Code:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
//[[NSUserDefaults standardUserDefaults] setInteger:high_level_value forKey:@"high_score"];
			//[[NSUserDefaults standardUserDefaults] synchronize];
}
As you can tell im fairly new to this, ive looked around but cant seem to wrap my head around it. Any help would be appreciated, thanks.
jbullfrog is offline   Reply With Quote
Old 10-10-2009, 09:43 PM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Do you have a pointer to your controller in your appdelegate? Then you can put the "save score" stuff in a method in the controller, and call [myController saveScore] from applicationWillTerminate.

If not, you can register your controller to get applicationWillTerminate messages with code like this in viewDidLoad:

Code:
	[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(saveScore) name:UIApplicationWillTerminateNotification object:nil];
You don't want to save the score in applicationDidFinishLaunching - that's when the application is starting, not ending.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 10-10-2009, 11:10 PM   #3 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 140
Default

Quote:
Originally Posted by smasher View Post
Do you have a pointer to your controller in your appdelegate? Then you can put the "save score" stuff in a method in the controller, and call [myController saveScore] from applicationWillTerminate.

If not, you can register your controller to get applicationWillTerminate messages with code like this in viewDidLoad:

Code:
	[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(saveScore) name:UIApplicationWillTerminateNotification object:nil];
You don't want to save the score in applicationDidFinishLaunching - that's when the application is starting, not ending.
Im not sure I Do you have a pointer to my controller in my appdelegate, I do have this

#import "testViewController.h"

Also I tried to place this code in the viewDidLoad, but didnt seem to do anything.

Code:
- (void)viewDidLoad {

	//high_level.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"high_score"];
	[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(high_level) name:UIApplicationWillTerminateNotification object:nil];
	
	
	//[[NSUserDefaults standardUserDefaults] setInteger:high_level_value forKey:@"high_score"];
	//[[NSUserDefaults standardUserDefaults] synchronize];
	 
	//high_level = [NSString stringWithFormat:@"high_score"];
}

Is there a place that I can check to see if anything is being written to it?
If so I could check and maybe I'm just not retrieving it properly.

So far Im not having any lucj saving this darn number and retrieving it later.
jbullfrog is offline   Reply With Quote
Old 10-10-2009, 11:48 PM   #4 (permalink)
iPhone Developer
 
kohjingyu's Avatar
 
Join Date: May 2009
Location: Singapore
Posts: 326
Default

You'd want to do this:

Code:
- (void)viewDidLoad
{
       //Load data here

	UIApplication *app = [UIApplication sharedApplication];
	[[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(applicationWillTerminate:)
												 name:UIApplicationWillTerminateNotification
											   object:app];
	
    [super viewDidLoad];
}

- (void)applicationWillTerminate:(NSNotification *)notification
{
       //Save data here
}
Hope that helps.
__________________
Bacteria Bash
Cheese Collect
Jokestar
Follow me on Twitter for news about my apps:
(Website|Twitter)
kohjingyu is offline   Reply With Quote
Old 10-11-2009, 12:15 AM   #5 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 140
Default

Quote:
Originally Posted by kohjingyu View Post
You'd want to do this:

Code:
- (void)viewDidLoad
{
       //Load data here

	UIApplication *app = [UIApplication sharedApplication];
	[[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(applicationWillTerminate:)
												 name:UIApplicationWillTerminateNotification
											   object:app];
	
    [super viewDidLoad];
}

- (void)applicationWillTerminate:(NSNotification *)notification
{
       //Save data here
}
Hope that helps.
I tried this and I don't get any errors or crashes, but it doesnt seem to save the high score label and load it back in when quit and reopened. What am i doing wrong here?

Code:
- (void)viewDidLoad
{
       //Load data here
if ([[NSUserDefaults standardUserDefaults] integerForKey:@"high_score"]) {
		high_level_value=[[NSUserDefaults standardUserDefaults] integerForKey:@"high_score"];

	UIApplication *app = [UIApplication sharedApplication];
	[[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(applicationWillTerminate:)
												 name:UIApplicationWillTerminateNotification
											   object:app];
	
    [super viewDidLoad];
}

- (void)applicationWillTerminate:(NSNotification *)notification
{
       //Save data here
[[NSUserDefaults standardUserDefaults] setInteger:high_level_value forKey:@"high_score"];
		[[NSUserDefaults standardUserDefaults] synchronize];
}
jbullfrog is offline   Reply With Quote
Old 10-11-2009, 12:52 AM   #6 (permalink)
iPhone Developer
 
kohjingyu's Avatar
 
Join Date: May 2009
Location: Singapore
Posts: 326
Default

Code:
- (void)viewDidLoad
{
       //Load data here
if ([[NSUserDefaults standardUserDefaults] integerForKey:@"high_score"]) {
		high_level_value=[[NSUserDefaults standardUserDefaults] integerForKey:@"high_score"];

	UIApplication *app = [UIApplication sharedApplication];
	[[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(applicationWillTerminate:)
												 name:UIApplicationWillTerminateNotification
											   object:app];
	
    [super viewDidLoad];
}

- (void)applicationWillTerminate:(NSNotification *)notification
{
       //Save data here
[[NSUserDefaults standardUserDefaults] setInteger:high_level_value forKey:@"high_score"];
		[[NSUserDefaults standardUserDefaults] synchronize];
}
There are a few errors in your code here.

Firstly, your if statement doesn't compare an integer to another integer.

Try this:

Code:
- (void)viewDidLoad
{
       //Load data here
high_level_value=[[NSUserDefaults standardUserDefaults] integerForKey:@"high_score"];

	UIApplication *app = [UIApplication sharedApplication];
	[[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(applicationWillTerminate:)
												 name:UIApplicationWillTerminateNotification
											   object:app];
	
    [super viewDidLoad];
}
Hope that helps.
__________________
Bacteria Bash
Cheese Collect
Jokestar
Follow me on Twitter for news about my apps:
(Website|Twitter)
kohjingyu is offline   Reply With Quote
Old 10-11-2009, 10:07 AM   #7 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 140
Default

Quote:
Originally Posted by kohjingyu View Post
Code:
- (void)viewDidLoad
{
       //Load data here
if ([[NSUserDefaults standardUserDefaults] integerForKey:@"high_score"]) {
		high_level_value=[[NSUserDefaults standardUserDefaults] integerForKey:@"high_score"];

	UIApplication *app = [UIApplication sharedApplication];
	[[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(applicationWillTerminate:)
												 name:UIApplicationWillTerminateNotification
											   object:app];
	
    [super viewDidLoad];
}

- (void)applicationWillTerminate:(NSNotification *)notification
{
       //Save data here
[[NSUserDefaults standardUserDefaults] setInteger:high_level_value forKey:@"high_score"];
		[[NSUserDefaults standardUserDefaults] synchronize];
}
There are a few errors in your code here.

Firstly, your if statement doesn't compare an integer to another integer.

Try this:

Code:
- (void)viewDidLoad
{
       //Load data here
high_level_value=[[NSUserDefaults standardUserDefaults] integerForKey:@"high_score"];

	UIApplication *app = [UIApplication sharedApplication];
	[[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(applicationWillTerminate:)
												 name:UIApplicationWillTerminateNotification
											   object:app];
	
    [super viewDidLoad];
}
Hope that helps.
Just tried that and got nothing, no errors, or crashes, just not loading in the high level when i restart. Just a question, this should work in the sim right, its not something that must be on the device?
jbullfrog is offline   Reply With Quote
Old 10-11-2009, 10:50 AM   #8 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 140
Default

I noticed that when i run it on the device and have console open, when the home button is pressed the last thing to register is

Debugger stopped.
Program exited with status value:0.
jbullfrog is offline   Reply With Quote
Old 10-11-2009, 10:34 PM   #9 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 140
Default

Im stumped any thoughts as to why this isn't working?
jbullfrog is offline   Reply With Quote
Old 10-12-2009, 01:42 AM   #10 (permalink)
Registered Member
 
Join Date: Aug 2009
Location: Lahore, Pakistan
Posts: 32
Send a message via MSN to stolidimran Send a message via Yahoo to stolidimran Send a message via Skype™ to stolidimran
Lightbulb

Noticed a minor issue, replace this

Code:
//SAVE HIGH LEVEL
	
[[NSUserDefaults standardUserDefaults] setInteger:high_level_value forKey:@"high_score"];
With this

Code:
//SAVE HIGH LEVEL
	
[[NSUserDefaults standardUserDefaults] setInteger:[high_level_value intValue] forKey:@"high_score"];
Hope this helps!
__________________
“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” Anonymous

Follow me on Twitter
http://twitter.com/stolidimran
stolidimran is offline   Reply With Quote
Old 10-12-2009, 09:07 AM   #11 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 140
Default

Quote:
Originally Posted by stolidimran View Post
Noticed a minor issue, replace this

Code:
//SAVE HIGH LEVEL
	
[[NSUserDefaults standardUserDefaults] setInteger:high_level_value forKey:@"high_score"];
With this

Code:
//SAVE HIGH LEVEL
	
[[NSUserDefaults standardUserDefaults] setInteger:[high_level_value intValue] forKey:@"high_score"];
Hope this helps!
When i do that i get this warning: invalid receiver type 'NSInteger'

Also and now im really stumped, when the player gets hit by the enemy, or when you collect the energy the value for the high level jumps from 0 to 264...

And this only seems to be happening in the simulator, not on the device.
jbullfrog is offline   Reply With Quote
Old 10-12-2009, 09:28 AM   #12 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 140
Default still not working right

I somehow got rid of that 264...

And i noticed if I do this
Code:
[[NSUserDefaults standardUserDefaults] setInteger:[high_level.text intValue] forKey:@"high_score"];
Instead of this, as you mentioned stolidimran
Code:
[[NSUserDefaults standardUserDefaults] setInteger:high_level_value forKey:@"high_score"];
I dont get any warnings, but still not savint the high level value.

I am doing this in the game loop,
Code:
//COMPARE CURRENT LEVEL WITH HIGH LEVEL. 
		if([current_level.text intValue] > [high_level.text intValue]){
			
			high_level.text = current_level.text;
			
		}
And that is working fine, as you play the game its setting the high level to the current level if the current level is greater than the high level.

So im assuming that when the game is closed, the high level value should be saving with this.
Code:
	 - (void)applicationWillTerminate:(NSNotification *)notification
	{
		
		//Save data here
		[[NSUserDefaults standardUserDefaults] setInteger:high_level_value forKey:@"high_score"];
		[[NSUserDefaults standardUserDefaults] synchronize];
		NSLog(@"app quit, save high level");
	}
And loading with this when restarting
Code:
- (void)viewDidLoad {
	//Load data here
	high_level_value=[[NSUserDefaults standardUserDefaults] integerForKey:@"high_score"];
	NSLog(@"set high level");
	
	
	UIApplication *app = [UIApplication sharedApplication];
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:app];
	NSLog(@"check close high level");
 	
[super viewDidLoad];

}
but it just doesnt work..
jbullfrog is offline   Reply With Quote
Old 10-12-2009, 01:37 PM   #13 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 140
Default

Ok, one small step for my app, one big step for me!

After thinking some more I solved my issue! I wasnt declaring the NSUserDefaults in the .h, so I set this in the ViewController.h
Code:
NSUserDefaults *prefs;
@property (nonatomic, retain) NSUserDefaults *prefs;
and in the ViewController.m

Code:
- (void)viewDidLoad {

	self.prefs = [NSUserDefaults standardUserDefaults];
	
	// Check to see what was stored in the user prefs, and update the label with that colour.
	if ([prefs stringForKey:@"high_score"]) {
		NSLog(@"The prefs is set.");
		high_level.text = [prefs stringForKey:@"high_score"];
	}

	high_level_value=[[NSUserDefaults standardUserDefaults] integerForKey:@"high_score"];
	NSLog(@"set high level");
	
	
	UIApplication *app = [UIApplication sharedApplication];
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:app];
	NSLog(@"check close high level");
 [super viewDidLoad];
}

	 - (void)applicationWillTerminate:(NSNotification *)notification
	{
		
		//Save data here
		[[NSUserDefaults standardUserDefaults] setInteger:[high_level.text intValue] forKey:@"high_score"];
		
		[[NSUserDefaults standardUserDefaults] synchronize];
		NSLog(@"app quit, save high level");
	}
Thanks for all the help
jbullfrog is offline   Reply With Quote
Reply

Bookmarks

Tags
nsuserdefaults, save, saving, score, uilabel

Thread Tools
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
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 256
18 members and 238 guests
2WeeksToGo, @sandris, AdamL, ADY, BrianSlick, Dani77, Dattee, GHuebner, headkaze, mer10, prchn4christ, Rudy, smithdale87, Thompson22, timle8n1, Touchmint, vigu360
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,880
Threads: 89,228
Posts: 380,747
Top Poster: BrianSlick (7,129)
Welcome to our newest member, @sandris
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:53 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0