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

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

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

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

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

Reply
 
LinkBack Thread Tools Display Modes
Old 07-23-2011, 01:16 AM   #1 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 30
iphonedevonthemake is on a distinguished road
Default sending bool values between views via Singleton

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];*/
	
	
}

Last edited by iphonedevonthemake; 07-23-2011 at 01:22 AM.
iphonedevonthemake is offline   Reply With Quote
Old 07-25-2011, 08:20 AM   #2 (permalink)
Registered Member
 
Join Date: Dec 2009
Posts: 3
iphonegdev is on a distinguished road
Default

Quote:
Originally Posted by iphonedevonthemake View Post
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];*/
	
	
}
Hi there,

From the first point of view I see some problems in your source code:
1. @property (nonatomic) IBOutlet BOOL isgamesaved - you don't need to use IBOutlet keyword because it's not an Interface Builder class.

2. property - is the instance's attribute. so, you cannot set/get it's value in class methods. you should do something like this:

+ (BOOL) isGameSaved{
return [[SingletonBOOLValues sharedBoolValues] isgamesaved];
}

+ (void) setGameSavedBOOL)boolValue{
[[SingletonBOOLValues sharedBoolValues] setIsgamesaved:boolValue];
}


Best regards,
Gennadii
iphonegdev is offline   Reply With Quote
Reply

Bookmarks

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: 409
11 members and 398 guests
AppleDev, chemistry, Emy, Gi-lo, hussain1982, ipodphone, mistergreen2011, pipposanta, Retouchable, skrew88, SLIC
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,679
Threads: 94,128
Posts: 402,923
Top Poster: BrianSlick (7,990)
Welcome to our newest member, xzoonxoom
Powered by vBadvanced CMPS v3.1.0

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