objective-c - 不明白Apple的takeFloatValueFrom : Example

标签 objective-c xcode macos cocoa

我不是 iOS 专家,但我对构建应用程序有足够的了解。我了解并理解模式、UIKit 和 Objective-C。我现在正在学习 Mac 开发和这一点 "Cocoa Bindings Programming Topics"让我难住了:

Take as an example a very simple application in which the values in a text field and a slider are kept synchronized. Consider first an implementation that does not use bindings. The text field and slider are connected directly to each other using target-action, where each is the other’s target and the action is takeFloatValueFrom: as shown in Figure 2. (If you do not understand this, you should read Getting Started With Cocoa.)

This example illustrates the dynamism of the Cocoa environment—the values of two user interface objects are kept synchronized without writing any code, even without compiling.

(强调我的)

嗯?不需要开个分店吗?和一个 IBAction 类似

- (IBAction)takeFloatValueFrom:(id)sender {
    self.slider.floatValue = [sender floatValue];
    self.textField.floatValue = [sender floatValue];
} 

这是 Mac 特有的东西吗?如何在不编写任何代码的情况下将两个控件与 XIB 中的目标操作实际连接起来并锁定它们的值?

最佳答案

当您在 Interface Builder 中设置界面时,您可以指定只要另一个对象以某种方式发生更改,它就向另一个对象发送消息。此示例显示的是,您可以将这两个对象连接起来,以便每当 slider 发生变化时,它都会将消息 takeFloatValueFrom: 发送到文本字段,反之亦然。

takeFloatValueFrom:是在 NSControl 上定义的方法,文本字段和 slider 都是 NSControl 的子类。

关于objective-c - 不明白Apple的takeFloatValueFrom : Example,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26287524/

相关文章:

ios - 如何将接收方的 ACCEPT 和 DECLINE 操作通知发送方?

iphone - UITextField rightView 上的触摸事件

objective-c - iOS:在位置服务后台运行时更新 UI

xcode - 你如何在 xcode 上快速调试断点?

c++ - 如何读写输入文件和输出文件

swift - 执行终端命令时无法访问文件

iphone - 如何安全地使用最近才引入 iOS 的对象的属性?

iOS mapview 使用 word 搜索位置

xcode - 为什么所有文件的 Targets 成员资格复选框都被禁用?

macos - 如何在 mac os x 10.7 中移动默认用户子目录 "Sites"?