我们什么时候需要按钮,在斯威夫特送说法? [英] When do we need button to send argument in Swift?

查看:127
本文介绍了我们什么时候需要按钮,在斯威夫特送说法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当连接在X code作为@IBAction FUNC按钮,我们可以参数之间进行选择:发件人或无

When connecting a button in Xcode as @IBAction func, we can choose between Arguments: Sender or None.

当我们做选择发件人,当无?

When do we choose Sender and when None?

推荐答案

好了,如果动作方法需要知道哪个按钮触发它,或者可以使用任何按钮的属性的,那么你就需要通过发件人(按钮),以该方法

Well, if the action method needs to know which button triggered it, or can make use of any of the button's properties, then you would need to pass the sender (the button) to the method.

如果你有一个计算器,其中有一个+和这方面的一个人为的例子是。他们都可以共享操作方法 performCalculation(发件人:NSButton ),方法可以使用​​的发件人的符号(这将是按钮的标题)来执行正确的计算。

A contrived example for this would be if you have a calculator, which has a '+' and a '-' button. They both could share the action method performCalculation(sender: NSButton) and the method could use the sender's symbol (this would be the button's title) to perform the correct calculation.

在另一方面,如果你有一个按钮,这将例如明确对文本框的所有输入,那么你就收线此按钮的方法 clearInput()这将不需要对触发它的按钮的任​​何信息(事实上,这里有没有参数将使它更容易在你的code在其他地方重复使用此方法,并触发编程方式清除文本框)。

On the other hand, if you have a button, that will for example clear all input on a textfield, then you would wire this button up to a method clearInput() which would not need to have any information about the button that triggered it (in fact, having no argument here would make it easier to reuse this method elsewhere in your code and trigger clearing the textfield programmatically).

我希望这对你有意义。有没有明确的规则AFAIK。它主要是品味问题,我猜。

I hope this makes sense to you. There are no clear rules afaik. It's mostly a matter of taste I guess.

这篇关于我们什么时候需要按钮,在斯威夫特送说法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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