是否可以将相同的操作方法分配给多个 Cocoa UI 对象(例如 NSButton)?
我目前正在学习 ObjC 和 Cocoa 编程,来自 Java 世界。 为了测试我当前的技能和学习进度,我从头开始创建一个小型计算器应用程序(OSX 而不是 iOS)。
我的 UI 有 10 个数字按钮 0-9 等。
我的第一个想法是,既然该操作收到了发件人的引用,就要做一个操作 就像 -(IBAction)captureDigit:(id)sender
一样,然后从按钮标题中获取数字。 但界面构建器似乎只允许一项操作与一个发送者连接。
所以我最终在控制器中创建了 10 个 captureDigit 操作。
我的问题: 第一个选项有可能吗?我想以编程方式将操作添加到按钮(这可能吗?),但随后我必须将所有数字按钮作为插座添加到我的控制器。
奖金问题: NSButton 可以保存某种不可见的值吗?在文档中找不到这个。 也许这会违反 MVC 模式,因为 UI 会知道应用程序特定的数据?
感谢您提前提供任何有用且友善的答案,我仍在学习
I'm currently learning ObjC and Cocoa programming, coming from the Java world.
To test my current skills and learning progress I'm creating a small calculator app from scratch (OSX not iOS).
My UI has 10 digit buttons 0-9 among others.
My first thought was, since the action receives the senders reference, to make one action
like -(IBAction)captureDigit:(id)sender
and then just grab the digit from the button title.
But the interface builder only allows an action to be connected with one sender it seems.
So I ended up creating 10 captureDigit actions in my controller.
My Question:
is the first option possible somehow? I thought of adding the actions programmatically (is this possible?) to the buttons, but then I would have to add all digit buttons as outlets to my controller.
Bonus Question:
can a NSButton hold some kind of non visible value? Could not find this in the documentation.
Maybe this would violate the MVC pattern as the UI would then know of application specific data?
Thanks for any useful and kind answer in advance, I'm still learning
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您按住 Control 键并从发件人拖动到目标,则可以将多个发件人连接到一个目标/操作,因此这不是问题。
WRT 你的额外问题,任何 NSView 都有一个整数
标签
,你可以在 Interface Builder 中设置它。这是区分多个相似视图的便捷方法。You can connect many senders to one target/action if you Control-drag from senders to the target, so that's not a problem.
WRT your bonus question, any NSView has an integer
tag
which you can set in Interface Builder. That's a convenient way to differentiate multiple similar views.您绝对可以将多个按钮连接到一个操作。此外,您还可以使用任何对象的
tag
字段为其赋予“幕后”值。You can definitely connect more than more button to a single action. Also, you can use the
tag
field of any object to give it a "behind the scenes" value.向单个控制器添加尽可能多的操作是完全可能的。 Interface Builder 如何阻止您这样做?
您的控制器中可以有一个 NSDictionary 实例,您可以在其中将 NSButtons 与您想要的任何数据相匹配。
It's perfectly possible to add as many actions to a single controller. How is Interface Builder preventing you from doing this?
You could have a NSDictionary instance in your controller, in which you could match NSButtons to whatever data you want.
为了简单起见,在 IB 中创建一个按钮并从 NSButton 拖动到 File 的所有者,然后它显示我们可以发送到 NSButton 的所有方法,然后选择 captureDigit:。现在复制并粘贴按钮更改标题,在IB中复制并粘贴保持连接并使用标签字段作为costique,nitrex已经说过了。
To make it easy, in IB create one button and drag from NSButton to File's owner it then shows all of the methods that we can send to NSButton, then select captureDigit:. Now copy and paste the button change the title, copy and paste in IB keeps the connection and use tag field as costique, nitrex have already said.