Reputation: 51
I've a little problem and I don't know how solve it:
I've develop a little game in SKspritekit and I would detect when the BOOL @property (play/pause) change status. I've a viewcontroller (UIViewController class) a superviewcontroller of all game, a singleton called BackgroundCommon (NSObject class) that include a @property BOOL pause and where is stored all methods for common background, and SKSCENEs that write a @proprety of backGroundCommon class.
When SKSCENE will change or a button will press... the system will write the @property . I find this solution that apparently works but nothing works for me. Link to official documentation
If I follow this guide the problems are:
backgroundcommon class is not instancied because this class is used to store methods
for me the observer would like be a ViewController and here I must instace a backgroundcommon class(i'm not sure)
is my viewController Bank object (like official documentation)?
is my backgroundCommon person object (like official documentation)?
I tried this on ViewController:
- (void)viewDidLoad {
[super viewDidLoad];
BackGroundCommon *bkc = [[BackGroundCommon alloc] init];
[bkc addObserver:self forKeyPath:@"pause" options:NSKeyValueObservingOptionNew context:NULL];
}
Then I added this (even on view controller):
-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { NSLog (@"working"); }
I tried to change the @property but this message appear in console:
An instance 0xc216b40 of class BackGroundCommon was deallocated while key value observers were still registered with it. Observation info was leaked, and may even become mistakenly attached to some other object. Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. Here's the current observation info: ( Context: 0x0, Property: 0xc218a40>
I don't understang where put it and how can use it... Please help me
Upvotes: 0
Views: 1840
Reputation: 101
You need to put your observer inside BackGroundCommon:
- (void)viewDidLoad {
[super viewDidLoad];
[self addObserver:self forKeyPath:@"self.pause" options:NSKeyValueObservingOptionNew context:nil];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
NSLog (@"working");
}
Later, to get the value you can use in other controller:
BackGroundCommon *bkc = [[BackGroundCommon alloc] init];
NSLog(@"PAUSE VALUE: %i", bkc.pause");
Upvotes: 1
Reputation: 1499
It seems the BKC object is deallocated, and its observers with it. if the observeValueForKeyPath is in the viewcontroller, just change [bkc addObserver ...] to [self addObserver ...].
also don't forget to remove the observer on the viewController's -(void)dealloc call.
Upvotes: 0