Re: Where to put global variables in Cocoa?
Re: Where to put global variables in Cocoa?
- Subject: Re: Where to put global variables in Cocoa?
- From: j o a r <email@hidden>
- Date: Fri, 7 Mar 2003 10:22:06 +0100
On Friday, Mar 7, 2003, at 09:57 Europe/Stockholm, Peter Karlsson wrote:
What do you mean here? If I for example want to save a midi channel so
I can use that value anywhere in my code. Any code example?
I already gave you a code example on how to do this off-list, didn't
I... ;)
If you need to have a midi channel available from anywhere in your
project, just add the appropriate methods to the MidiManager class
inlcuded below:
@interface DataManager : NSObject
{
@private
int _someValue;
}
+ (id) singletonInstance;
- (int) theValue;
- (void) setTheValue:(int) value;
@end
@implementation DataManager
+ (id) singletonInstance
{
static id sharedDataManager = nil;
if (sharedDataManager == nil)
{
sharedDataManager = [[self alloc] init];
}
return sharedDataManager;
}
- (int) theValue
{
return _someValue;
}
- (void) setTheValue:(int) value
{
_someValue = value;
}
@end
In this way you could call your reset method like this:
- (IBAction)resetAction:(id)sender
{
[[DataManager singletonInstance] setTheValue: 0];
}
...or you might perfere to add another layer of abstraction:
@interface MidiManager : NSObject
+ (void) reset;
@end
@implementation MidiManager
+ (void) reset;
{
[[DataManager singletonInstance] setTheValue: 0];
}
@end
...and then it would be:
- (IBAction)resetAction:(id)sender
{
[MidiManager reset];
}
j o a r
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.