可可手工绑定

扫码查看
本文介绍了可可手工绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ImageView的,显示一个锁,如果打开的文件被锁定或不告知。我有2图像进行锁定和解锁的情况下。我想要显示的图像与我的对象重新presenting打开的文件的布尔值同步。

I have an ImageView which shows a lock, informing if an opened file is locked or not. I have 2 images for locked and unlocked cases. I want synchronize the displayed image with boolean value of my object representing an opened file.

要做到这一点,我想我要的ViewController根据对象的锁定状态更改我的ImageView图像。因此,无论对象和ViewController有一个属性isLocked。

To do this I want my ViewController to change the image in my ImageView depending on lock state of object. So both object and ViewController have a property "isLocked".

如何同步呢?这是很容易的,但IB我不知道如何做到这一点编程。我想在我的ViewController的初始化方法使用方法:

How can I synchronize them? It is easy in IB but I don't know how to do it programmatically. I tried in initialize method of my ViewController to use:

[ViewController bind:@"value" toObject:[ArrayController selection] withKeyPath:@"isLocked" options:nil];

但它不工作。在文档中,说我揭露我使用它之前绑定。

But it doesn't work. In documentation it is said that I have to expose my binding before using it.

我试图把下面的code在我对象的初始化方法:

I try to put the following code in initializer method of my object:

[self exposeBinding:@"isLocked"];

不过X code没有认识到这一点的方法。

But Xcode doesn't recognize this method.

有人是否有这种绑定建立的经验吗?

Does somebody have experience with this kind of bindings establishing?

推荐答案

由于@nick说,你想键 - 值观察。

As @nick says, you want Key-Value-Observing.

[arrayController addObserver:self
                forKeyPath:@"selection.isLocked"
                options:NSKeyValueObservingOptionNew
                context:@"this_context"]

然后当isLocked改变-observeValueForKeyPath:ofObject变化:背景:您已经添加到您的viewController将被调用的方法(只要你只有在一个KVC兼容的方式操纵isLocked)

Then when isLocked changes the -observeValueForKeyPath:ofObject:change:context: method that you have added to your viewController will be called (as long as you only manipulate isLocked in a KVC compliant way).

选项参数,可以选择性调整到底是什么条件将触发通知,哪些数据与该通知一起发送。上下文参数是有帮助你通知你注册的接收和通知超类注册接收区分开来。这是可选的。

The options parameter lets you optionally tweak exactly what conditions will trigger the notification and what data is sent along with the notification. The context parameter is there to help you distinguish between notifications that you registered to receive and notifications your superclass registered to receive. It is optional.

绑定看起来像他们可能是保持两个值同步有用。然而,这是不是他们都做什么

Bindings seem like they might be useful to keep two values in sync. However, this is not what they do at all.

是的,很多事情似乎给IM pression,这是他们做什么,并没有太多说这是不是他们做了什么,也有很多人认为,这是他们做 - 但没有,你不能使用他们的这一

Yes, lots of things seem to give the impression that this is what they do, and there isn't much saying that this isn't what they do, also lots of people believe that this is what they do - but no, you cannot use them for this.

只有类的支持绑定极少数(they这里列出)然后,,这是最重要的一点,这些类只支持绑定自己命名的绑定的,并且这些绑定不是实例变量。例如,的NSTextField 有一个' fontFamilyName '尚未绑定的NSTextField的没有fontFamilyName属性或实例变量,甚至派生的。的NSTextField确实有一个'isBordered财产,但不具有约束力的 - 所以你不能绑定'isBordered

Only a handful of classes support bindings (they are listed here) and then, and this is the important bit, those classes only support binding their named bindings, and these bindings are not instance variables. eg NSTextField has a 'fontFamilyName' binding yet NSTextField does not have a 'fontFamilyName' property or instance variable, even a derived one. NSTextField does have a 'isBordered' property but not a binding - so you cannot bind 'isBordered'.

这并不意味着什么绑定的任意类的任意属性。

It does not mean anything to 'bind' an arbitrary property of an arbitrary Class.

这篇关于可可手工绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 05:39
查看更多