I try to call an IBOutlet, which is a property of a Singleton Object from another class but it returns nil. I´ve read a lot of of other posts about singletons here but there seems to be something that I´m missing. This is a OSX question.
In the Interface Builder I have a Button and an Object with a Singleton set as custom class. The Singletons class is called "CEOptionsST". The button is connected as IBOutlet to the Singleton´s class header.
@property (weak) IBOutlet NSButton *mybutton;
Later I´d like to access the button´s state from another class. Therefore I call:
CEOptionsST *opt = [CEOptionsST sharedOptions];
NSLog(@"state: %ld",(long)[[_opt mybutton] state]);
This always returns 0 no matter if the button is on or off. Using Textfields instead of buttons returns nil.
The pointer is the same when I log self
from inside the Singleton´s and opt
. So the Singleton is not instanced twice.
When I check opt
in debug mode it tells me that all properties are nil. The IBOutlet property "mybutton" is nil. So the Singleton Object initiated by the Interface Builder (or nib) is the same as opt
but the state of mybutton and other properties is not accessible - what am I missing??
What I´m trying to achieve is to have UI controls as "options" for the user, which are accessible throughout the application.
Here´s my Singleton class (by Dave DeLong´s answer):
@interface CEOptionsST : NSObject
+ (id) sharedOptions;
@property (weak) IBOutlet NSButton *mybutton;
@end
@implementation CEOptionsST
+ (void)initialize{
[self sharedOptions];
}
+ (id)sharedOptions {
static CEOptionsST *sharedOptions = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedOptions = [[self actualAlloc] initActual];
});
return sharedOptions;
}
+ (id)actualAlloc {
return [super alloc];
}
+ (id)alloc {
return [CEOptionsST sharedOptions];
}
- (id)initActual {
self = [super init];
if (self) {
}
return self;
}
- (id)init {
return self;
}
- (id)initWithCoder:(NSCoder *)decoder {
return self;
}
Thanks, Thomas