简体   繁体   English

如何在属性目标c中检测更改状态

[英]How detect change status @property objective c

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. 我在SKspritekit中开发了一个小游戏,可以检测BOOL @property(播放/暂停)何时更改状态。 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. 我有一个viewcontroller(UIViewController类),所有游戏的superviewcontroller,一个名为BackgroundCommon(NSObject类)的单例,其中包括一个@property BOOL暂停,并在其中存储了通用背景的所有方法,还有一个SKSCENE,它编写了一个@proprety的backGroundCommon类。

When SKSCENE will change or a button will press... the system will write the @property . 当SKSCENE将更改或按下按钮时,系统将写入@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 没有实例化backgroundcommon类,因为该类用于存储方法

  • for me the observer would like be a ViewController and here I must instace a backgroundcommon class(i'm not sure) 对我来说,观察者想成为一个ViewController,在这里我必须安装一个backgroundcommon类(我不确定)

  • is my viewController Bank object (like official documentation)? 是我的viewController Bank对象(例如官方文档)吗?

  • is my backgroundCommon person object (like official documentation)? 是我的backgroundCommon Person对象(例如官方文档)吗?

I tried this on ViewController: 我在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: 我试图更改@property,但此消息出现在控制台中:

An instance 0xc216b40 of class BackGroundCommon was deallocated while key value observers were still registered with it. 在仍然向键值观察者注册的同时,释放了BackGroundCommon类的实例0xc216b40。 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. 在NSKVODeallocateBreak上设置一个断点以在调试器中停止。 Here's the current observation info: ( Context: 0x0, Property: 0xc218a40> 这是当前的观察信息:(上下文:0x0,属性:0xc218a40>

I don't understang where put it and how can use it... Please help me 我不明白它放在哪里以及如何使用它...请帮助我

You need to put your observer inside BackGroundCommon: 您需要将观察者放入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");

It seems the BKC object is deallocated, and its observers with it. 似乎BKC对象已被释放,它的观察者也已释放。 if the observeValueForKeyPath is in the viewcontroller, just change [bkc addObserver ...] to [self addObserver ...]. 如果observeValueForKeyPath在视图控制器中,则只需将[bkc addObserver ...]更改为[self addObserver ...]。

also don't forget to remove the observer on the viewController's -(void)dealloc call. 也不要忘记删除viewController的-(void)dealloc调用上的观察者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM