两个类之间的iPhone KVO [英] iPhone KVO between two classes

查看:94
本文介绍了两个类之间的iPhone KVO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序类A和类B中有两个类.A类和B类都是UIViewController的实例。 A类有一个按钮,推送时将B类推入堆栈。 B类有一个A类想要观察的字符串,并根据需要更新它的界面。我已经能够使用:
[self addObserver:self forKeyPath:@nameoptions:0 context:NULL]; 在B类中查看更改字符串。
当我在A类中尝试使用以下内容时,viewWillAppear方法:

I have two classes in my app class A and Class B. Both class A and B are instances of UIViewController. Class A has a button that when pushed pushes class B onto the stack. Class B has a string that class A would like to observe and update it's interface with as needed. I've been able to use: [self addObserver:self forKeyPath:@"name" options:0 context:NULL]; in class B to view the changes to the string. When i try and use the following in class A viewWillAppear method:

ClassB *b = [[ClassB alloc]init];
[b addObserver:self forKeyPath:@"name" options:0 context:NULL];

并添加方法:

(void)observeValueForKeyPath:(NSString )keyPath ofObject:(id)object
                      change:(NSDictionary )change
                     context:(void )context

尝试查看B中的B更新时,没有触发任何操作。我觉得这个问题很傻,但KVO如何处理iOS中有两个类?我知道这应该可行。

No action is fired when trying to view the updates made in B from A. I feel silly asking this question but how does KVO work between two classes in iOS? I know this should work.

推荐答案

您可以观察不同对象/类之间的变化。我认为问题出在 addObserver:forKeyPath:options:options:的选项参数中。

You can observe changes across different objects/classes. I think the problem is in the options parameter of addObserver:forKeyPath:options:context:.

有各种各样的您想要做的观察类型的选项。 KVO指南是一个很好的起点,但是你可能想要 NSKeyValueObservingOptionNew ,我在下面的例子中使用。

There are various options for the type of observing you want to do. The KVO Guide is a good starting point, but you probably want NSKeyValueObservingOptionNew, which I use in the example below.

首先,name应该是公共财产在ClassB。

First, "name" should be a public property in ClassB.

其次,您可能不需要在ClassA中的 viewWillAppear 中将观察者添加到b,因为每次出现ClassA视图时都不需要添加它。在创建ClassB视图时,您只需要添加一次观察者。一旦添加了观察者, observeValueForKeyPath:ofObject:change:context:方法将在ClassA中执行,因此您可以从那里对ClassA UI进行更新。每次ClassA即将出现时,您都不需要做任何事情。

Second, you probably don't need to add the observer to "b" in viewWillAppear in ClassA, because you don't need to add it everytime the ClassA view is going to appear. You just need to add the observer once, when you create the ClassB view. Once you've added the observer, the observeValueForKeyPath:ofObject:change:context: method will be executed in ClassA, so you can do the update to the ClassA UI from there. You shouldn't need to do anything every time ClassA is about to appear.

在A类中,您可能应该在将ClassB推送到控制器堆栈之前创建ClassB,可能是在用户采取某些操作的事件处理程序中。在创建ClassB之后,立即在ClassA中添加具有正确NSKeyValueObservingOption值的观察者。

In Class A, you should probably create ClassB just before you are going to push ClassB onto the controller stack, presumably in the event handler for some action the user took. Immediately after you create ClassB, add the observer in ClassA with the correct NSKeyValueObservingOption value.

如果您只是希望在ClassB中的公共属性name发生更改时收到通知,然后试试这个:

If you just want to be notified whenever the public property "name" in ClassB is changed, then try this:

ClassB

@interface ClassB : UIViewController {
}

@property (retain) NSString* name;

- (void) aMethodThatModifiesName:(NSString*)newName;

@end


@implementation ClassB 

@synthesize name;

- (void) aMethodThatModifiesName:(NSString*)newName {
    // do some stuff
    self.name = newName;
}

@end

ClassA

@interface ClassA : UIViewController {
}

@property (nonatomic, retain) IBOutlet UILabel* nameLabel;

- (IBAction) someEventHandler:(id)sender;

@end

@implementation ClassA

- (IBAction) someEventHandler:(id)sender {
    ClassB* b = [[ClassB alloc]init];
    [b addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:NULL];
    [self.navigationController pushViewController:b animated:YES];
    [b release];
}

- (void) observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context {
    if ([keyPath isEqual:@"name"]) {
        NSString* changedName = [change objectForKey:NSKeyValueChangeNewKey];
        // do something with the changedName - call a method or update the UI here
        self.nameLabel.text = changedName;
    }
}

@end

这篇关于两个类之间的iPhone KVO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆