Reputation: 37
I have searched around and cannot find anything, help would be appreciated. I am very new to objective-C and Xcode.
In my app the player starts with 100 coins, this is represented in a label. When the user clicks a button to spend 10 coins, a popup box appears and asks 'are you sure', the user can click ok, or cancel.
If the user clicks 'ok' they spend 10 coins. At the moment, in the simulator when i'm in the same view everything is fine, the 100 goes down to 90 etc... But when I go to another view and then back again, the coin amount goes back up to 100. This is the same when the user exits the app.
Here is my code:
.h file
//Coin
IBOutlet UILabel * coinCount;
.m file
int coinAmount = 100;
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
NSLog(@"user pressed Cancel");
// Any action can be performed here
}
else
{
NSLog(@"user pressed OK");
coinAmount -= 10;
[coinCount setText:[NSString stringWithFormat:@"%d", coinAmount]];
NSString * string = [NSString stringWithFormat:@"%d", coinAmount];
//Save coin amount
NSString * saveCoinAmount = string;
NSUserDefaults * defaultsCoinAmount = [NSUserDefaults standardUserDefaults];
[defaultsCoinAmount setObject:saveCoinAmount forKey:@"saveCoinLabel"];
[defaultsCoinAmount synchronize];
}
}
That seems to save the new coin amount, so now when the user goes to another view and back i try and load the saved coin amount:
- (void)viewDidLoad
{
[super viewDidLoad];
//Coin Label
NSUserDefaults * defaultsLoadCoin = [NSUserDefaults standardUserDefaults];
NSString * loadCoinLabel = [defaultsLoadCoin objectForKey:@"saveCoinLabel"];
[coinCount setText:loadCoinLabel];
}
Any help would be greatly appreciated!
Upvotes: 0
Views: 143
Reputation: 114974
Your problem is that you are storing your coins in two places - an integer variable and the label. When you return to your view you restore the saved coin amount directly into the label, but when you perform the "purchase" you use the integer, which has been re-initialised to 100.
I also suggest you get out of the habit of using instance variables and use properties.
You should do something like this -
.m file
@interface MyClass () // Change this to suit your class name
@property NSInteger coinAmount;
@property (weak,nonatomic) IBOutlet UILabel *coinLabel;
@end
@implementation MyClass
- (void)viewDidLoad
{
[super viewDidLoad];
//Coin Label
NSUserDefaults * defaultsLoadCoin = [NSUserDefaults standardUserDefaults];
if ([defaultsLoadCoin objectForKey:@"coins] == nil) {
self.coinAmount=100;
[defaultsLoadCoin setInteger:self.coinAmount forKey:@"coins"];
}
else {
self.coinAmount = [defaultsLoadCoin integerForKey:@"coins"];
}
self.coinLabel.text=[NSString stringWithFormat:@"%ld",self.coinAmount];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
NSLog(@"user pressed Cancel");
// Any action can be performed here
}
else
{
NSLog(@"user pressed OK");
self.coinAmount -= 10;
self.coinLabel.text=[NSString stringWithFormat:@"%ld",self.coinAmount];
//Save coin amount
NSString * saveCoinAmount = string;
NSUserDefaults * defaultsCoinAmount = [NSUserDefaults standardUserDefaults];
[defaultsCoinAmount setInteger:self.coinAmount forKey:@"coins"];;
[defaultsCoinAmount synchronize];
}
Upvotes: 1
Reputation: 6207
Your coinAmount
property is not persistent through app launches or initialisations of the view controller
in which it is created. You should consider persisting this value in database (like CoreData
) or in NSUserDefaults
.
My advice: start with basics (links to docs):
Upvotes: 0