Tag: 键值观察

观察UIDatePicker中的更改

我注意到没有委托来观察UIDatePicker中的更改。 有没有一种方法可以在拾取器发生变化而没有确认任何内容的情况下进行检测,例如旋转的时刻,以及我希望能够检测到的新数字。 我想到了关键的价值观,但是我不认为现场有一个变化的财产

核心数据:发送KVO通知,以获取瞬态派生属性

我有一个自定义类,有一个瞬态和派生(只读)属性称为DerivedProperty Parent实体。 DerivedProperty的值取决于DerivedProperty的值,因此只要IndependentProperty1发生更改, DerivedProperty的值就会改变。 但是, Parent与Child (称为children )具有多对多关系,并且DerivedProperty也依赖于Parent的所有Child对象中的IndependentProperty2的值。 因此,只要Parent IndependentProperty2或Child对象的IndependentProperty2发生更改,我想通知任何观察者DerivedProperty也已更改。 到目前为止,我已经到了以下代码。 唯一的问题是没有发出KVO通知为DerivedProperty因为如果我尝试在objectContextDidChange:做到这objectContextDidChange:那么代码将最终在一个循环。 – (void) awakeFromInsert { [super awakeFromInsert]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(objectContextDidChange:) name:NSManagedObjectContextObjectsDidChangeNotification object:self.managedObjectContext]; } – (void) awakeFromFetch { [super awakeFromFetch]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(objectContextDidChange:) name:NSManagedObjectContextObjectsDidChangeNotification object:self.managedObjectContext]; } – (void) objectContextDidChange: (NSNotification *) notification { NSSet *updatedObjects = [[notification userInfo] objectForKey:NSUpdatedObjectsKey]; if ([updatedObjects containsObject:self] || […]

如何用Swift和KVO观察单个数组元素的更改(更新)?

我是否需要订阅/取消订阅数组中的单个元素? 我想单独更新每个表视图单元格以反映后备数组中的更改。 通过更改我的意思是不追加/删除操作,但更新数组的对象的属性。 我希望我能够解释我想达到的目的。 谢谢

NSManagedObject属性数组

我想获得我的NSManagedObject的属性数组,所以我可以使用KVO导出它们。 我可以手动创build一个数组,然后遍历它,但是,我想自动获取这个列表,然后迭代。

简单的KVO例子

我正在试图做简单的KVO例子,但我有问题。 这是我的* .m文件: #import "KVO_ViewController.h" @interface KVO_ViewController () @property NSUInteger number; @end @implementation KVO_ViewController – (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self addObserver:self forKeyPath:@"number" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil]; } – (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } […]

这个observeValueForKeyPath有什么问题:ofObject:change:context:implementation?

在我的UIScrollView子类中,我正在观察帧的变化: [self addObserver:self forKeyPath:@"frame" options:0 context:NULL]; 我的observeValueForKeyPath:ofObject:change:context:实现如下: – (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if (object == self && [keyPath isEqualToString:@"frame"]) { [self adjustSizeAndScale]; } if ([UIScrollView instancesRespondToSelector:@selector(observeValueForKeyPath:ofObject:change:context:)]) { [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; // Exception } } 但是我得到这个代码的例外: *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '<WLImageScrollView: 0x733a440; baseClass = UIScrollView; […]

KVO修改由NSMutableArray支持的NSArray的通知

我正在尝试使用KVO来侦听NSArray属性上的集合更改事件。 公开的说,这个属性是一个只读的NSArray,但是由一个NSMutableArray ivar支持,所以我可以修改这个集合。 我知道我可以将属性设置为一个新值来获得“设置”更改,但我有兴趣添加,删除,replace更改。 如何正确地通知NSArray的这些types的更改? @interface Model : NSObject @property (nonatomic, readonly) NSArray *items; @end @implementation Model { NSMutableArray *_items; } – (NSArray *)items { return [_items copy]; } – (void)addItem:(Item *)item { [_items addObject:item]; } @end Model *model = [[Model alloc] init]; [observer addObserver:model forKeyPath:@"items" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:NULL]; Item *item = [[Item alloc] […]

两类之间的iPhone KVO

我在我的应用程序类A和类B中有两个类。类A和B都是UIViewController的实例。 类A有一个button,当按下时将类B推入堆栈。 B类有一个string,类A想要观察并根据需要更新它的接口。 我已经能够使用: [self addObserver:self forKeyPath:@"name" options:0 context:NULL]; 在类B中查看对string的更改。 当我尝试在类viewWillAppear方法中使用以下内容: 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 当试图从A中查看B中所做的更新时,没有任何操作被触发。我觉得这个问题很愚蠢,但是如何在iOS中的两个类之间工作? 我知道这应该工作。

键值在Swift中观察不显示数组中的插入和删除

我创build了一个包含数组的类。 我在视图控制器中添加了一个观察者,并对该数组进行了一些修改。 问题是,当我打印由observeValueForKeyPath()方法返回的更改字典时,我只能看到种类NSKeyValueChangeSetting的更改。 换句话说,该方法告诉我数组已经改变,为我提供了新旧数组(包含所有元素),但是我想要接收哪些特定项目被添加或删除的信息。 这里是一些示例代码。 这是数组将被观察的类。 private let _observedClass = ObservedClass() class ObservedClass: NSObject { dynamic var animals = [String]() dynamic var cars = [String]() class var sharedInstance: ObservedClass { return _observedClass } } 这是我的视图控制器的代码。 class ViewController: UIViewController { var observedClass = ObservedClass.sharedInstance required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder) observedClass.addObserver(self, forKeyPath: "animals", options: .New […]

了解iOS中的KVO

关于“确保KVO合规性”,有一些官方定义似乎很难理解 为了被视为符合KVO标准的特定财产,一个class级必须确保以下内容; 该类必须符合“确保KVC合规性”中指定的属性的键值编码。 该类必须允许属性的自动观察者通知,或者实现对属性的手动键值观察。 谁能给出更具体的例子来使这个更清楚? 谢谢