向NSString添加观察者 [英] Adding an observer to an NSString

查看:90
本文介绍了向NSString添加观察者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序委托中,我有一个NSString,可以通过类进行修改.我要做的是让另一个类观察此NSString并在更改此NSString时做出反应.

In my application delegate I have an NSString which can be modified by a class. What I want to do is to have another class observe this NSString and to react whenever this NSString is changed.

有人知道我该怎么做吗?

Does anybody know how I can do this?

推荐答案

在其他课程"上,您必须执行以下操作:

On the "other class" you must do something like:

[MyAppDelegate addObserver:self forKeyPath:@"VARIABLE_NAME" options:NSKeyValueObservingOptionNew context:nil];

并实施

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

这篇关于向NSString添加观察者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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