将观察者添加到 BOOL 变量

发布于 2024-11-01 01:57:45 字数 68 浏览 0 评论 0原文

是否可以将观察者添加到简单变量(例如 BOOL 或 NSIntegers)并查看它们何时发生变化?

谢谢!

Is it possible to add observers to simple variables such as BOOLs or NSIntegers and see when they change?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

长梦不多时 2024-11-08 01:57:45

您观察键,当它们的值发生变化时会收到通知。数据类型可以是任何类型。对于定义为 Objective-C 属性的任何内容(在 .h 文件中使用 @property),这已经准备就绪,因此,如果您想观察添加到视图控制器的 BOOL 属性,请按如下方式操作:

在 myViewController.h 中:

@interface myViewController : UIViewController {
    BOOL      mySetting;
}

@property (nonatomic)    BOOL    mySetting;

在 myViewController.m 中

@implementation myViewController

@synthesize mySetting;

// rest of myViewController implementation

@end

otherViewController.m 中:

// assumes myVC is a defined property of otherViewController

- (void)presentMyViewController {
    self.myVC = [[[MyViewController alloc] init] autorelease];
    // note: remove self as an observer before myVC is released/dealloced
    [self.myVC addObserver:self forKeyPath:@"mySetting" options:0 context:nil];
    // present myVC modally or with navigation controller here
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if (object == self.myVC && [keyPath isEqualToString:@"mySetting"]) {
        NSLog(@"OtherVC: The value of self.myVC.mySetting has changed");
    }
}

You observe keys to be notified when their value changes. The data type can be anything. For anything defined as an Objective-C property (with @property in the .h file) this is ready to go so if you want to observe a BOOL property you add to a view controller you do it as follows:

in myViewController.h:

@interface myViewController : UIViewController {
    BOOL      mySetting;
}

@property (nonatomic)    BOOL    mySetting;

in myViewController.m

@implementation myViewController

@synthesize mySetting;

// rest of myViewController implementation

@end

in otherViewController.m:

// assumes myVC is a defined property of otherViewController

- (void)presentMyViewController {
    self.myVC = [[[MyViewController alloc] init] autorelease];
    // note: remove self as an observer before myVC is released/dealloced
    [self.myVC addObserver:self forKeyPath:@"mySetting" options:0 context:nil];
    // present myVC modally or with navigation controller here
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if (object == self.myVC && [keyPath isEqualToString:@"mySetting"]) {
        NSLog(@"OtherVC: The value of self.myVC.mySetting has changed");
    }
}
最近可好 2024-11-08 01:57:45

我相信您的意思是:如果属性已更改,如何从“更改”字典中获取 INT 或 BOOL 值。

你可以简单地这样做:

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context
{
    if ([keyPath isEqualToString:@"mySetting"])
    {
        NSNumber *mySettingNum = [change objectForKey:NSKeyValueChangeNewKey];
        BOOL newSetting = [mySettingNum boolValue];
        NSLog(@"mySetting is %s", (newSetting ? "true" : "false")); 
        return;
    }

    [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}

I believe what you meant was: How to get INT or BOOL value from the 'change' dictionary if the property has changed.

You can simply do it this way:

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context
{
    if ([keyPath isEqualToString:@"mySetting"])
    {
        NSNumber *mySettingNum = [change objectForKey:NSKeyValueChangeNewKey];
        BOOL newSetting = [mySettingNum boolValue];
        NSLog(@"mySetting is %s", (newSetting ? "true" : "false")); 
        return;
    }

    [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
笑红尘 2024-11-08 01:57:45

是的;唯一的要求是这些变量所在的对象与这些属性的键值兼容。

Yes; the only requirement is that the object in which those variables occur are key-value compliant for those properties.

GRAY°灰色天空 2024-11-08 01:57:45

如果它们是对象的属性,那么是的。

如果它们不是属性,那么就不是。

If they are properties of objects then yes.

If they are not properties then no.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文