Hey!
I trying to send BOOL values between views but it fails for some reason. I tried the "dot" way (for example if I'm located in the settings menu and I want to access a BOOL value from my game.... then I tried like this : settings.m if(game.saveState = YES) //then a button enables))
I'm now trying to create a singleton class that holds the BOOL values between views since the "dot" way didn't work but my game crashes when it accesses the singleton class from inside the game.
what am I doing wrong?
Ps. I used this as a template
HTML Code:
http://www.devbypractice.com/reusable-singleton-class-in-objective-c-for-iphone-and-ipad/
SingletonBOOLValues.h
Code:
@interface SingletonBOOLValues : NSObject {
BOOL isgamesaved;
}
@property (nonatomic) IBOutlet BOOL isgamesaved;
+ (SingletonBOOLValues *)sharedBoolValues;
+ (BOOL) isGameSaved;
+ (void) setGameSaved:(BOOL)boolValue; @end
SingletonBOOLValues.m
Code:
#import "SingletonBOOLValues.h"
@implementation SingletonBOOLValues
@synthesize isgamesaved;
// This var will hold our Singleton class instance that will be handed to anyone who asks for it
static SingletonBOOLValues *sharedBoolValues = nil;
// Class method which provides access to the sharedBoolValues var.
+ (SingletonBOOLValues *)sharedBoolValues {
// synchronized is used to lock the object and handle multiple threads accessing this method at
// the same time
@synchronized(self) {
// If the sharedBoolValues var is nil then we need to allocate it.
if(sharedBoolValues == nil) {
// Allocate and initialize an instance of this class
[[self alloc] init];
}
}
// Return the sharedBoolValues
return sharedBoolValues;
}
/* This is called when you alloc an object. To protect against instances of this class being
allocated outside of the sharedBoolValues method, this method checks to make sure
that the sharedBoolValues is nil before allocating and initializing it. If it is not
nil then nil is returned and the instance would need to be obtained through the sharedBoolValues method
*/
+ (id)allocWithZone:(NSZone *)zone {
@synchronized(self) {
if (sharedBoolValues == nil) {
sharedBoolValues = [super allocWithZone:zone];
return sharedBoolValues; // assignment and return on first allocation
}
}
return nil; //on subsequent allocation attempts return nil
}
- (id)copyWithZone:(NSZone *)zone {
return self;
}
-(id)init
{
id _self_ = [super init];
if (_self_ != nil) {
self = _self_;
isgamesaved = NO;
}
return (_self_);
}
+ (BOOL) isGameSaved{
return isgamesaved;
}
+ (void) setGameSaved:(BOOL)boolValue{
isgamesaved = boolValue;
}
- (void)dealloc {
[super dealloc];
}
@end
and this is how I try to access them from the settings.m file and from my game.m file
settings.h
Code:
//Bool Values
IBOutlet SingletonBOOLValues *sharedBoolValues;
@property (nonatomic, retain) IBOutlet SingletonBOOLValues *sharedBoolValues;
settings.m
Code:
@synthesize sharedBoolValues;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
NSLog(@"Main Menu");
[super viewDidLoad];
//Init Bool
//sharedBoolValues = [SingletonBOOLValues sharedBoolValues];
}
//The game begins either by continue or by resetting the gamestate
-(IBAction)ResumeSavedGame{
BOOL savestate = [SingletonBOOLValues isGameSaved];
[SingletonBOOLValues setGameSaved:!savestate];
if(!savestate){
//if(game.MSaveToContinue == YES"){ //here you can see how I tried before
continuegame.hidden = NO;
continuegame.alpha = 1.0;
continuegame.enabled = YES;
}
else if(savestate){
//else if(game.MSaveToContinue == NO){ //here you can see how I tried before
continuegame.hidden = NO;
continuegame.alpha = 0.5;
continuegame.enabled = NO;
}
}
game.h
Code:
//Bool Values
IBOutlet SingletonBOOLValues *sharedBoolValues;
@property (nonatomic, retain) IBOutlet SingletonBOOLValues *sharedBoolValues;
game.m
Code:
@synthesize sharedBoolValues;
- (void)viewDidLoad {
NSLog(@"Main Menu");
[super viewDidLoad];
//Init Bool
//sharedBoolValues = [SingletonBOOLValues sharedBoolValues];
}
//Returns to MainMenu
-(IBAction)end{
GamePaused = YES;
menureturn = [[MainMenu alloc] initWithNibName:@"MainMenu" bundle: nil];
menureturn.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:menureturn animated: YES];
[self saveGameState];
//The part below makes the game to crash
BOOL savestate = [SingletonBOOLValues isGameSaved];
if(savestate = YES){
[SingletonBOOLValues setGameSaved:!savestate];
}
else {
[SingletonBOOLValues setGameSaved:savestate];
}
//tried to do this way as well
/*BOOL savestate = [SingletonBOOLValues isGameSaved];
savestate != savestate;
[SingletonBOOLValues setGameSaved:savestate];*/
}