I know the differences between IBAction and IBOutlet, but I don't know when I should use one over the other.
我知道IBAction和IBOutlet之间的区别,但我不知道何时应该使用一个而不是另一个。
For example, I'm trying to make a calculator. I have ten buttons and one label to display a result. Must I create ten IBOutlets in my interface? If not, why not?
例如,我正在尝试制作一个计算器。我有十个按钮和一个标签来显示结果。我必须在界面中创建十个IBOutlet吗?如果没有,为什么不呢?
3 个解决方案
#1
2
Ok, IBOutlets and IBActions serve two separate purposes.
好的,IBOutlets和IBActions有两个不同的用途。
IBActions are basically methods that can be connected to UI elements through IB. They provide ways for your object to be notified when something has happened with the UI. They also provide the sender argument when they are called so that you can access the UI Element that sent the message from within the method.
IBActions基本上是可以通过IB连接到UI元素的方法。它们提供了在UI发生某些事情时通知对象的方法。它们还在调用时提供sender参数,以便您可以访问从方法中发送消息的UI元素。
An IBOutlet on the other hand offers a way to get a reference to the UI element within your code at any point, it is used when you need to change aspects of the UI.
另一方面,IBOutlet提供了一种在任何时候获取代码中UI元素的引用的方法,它在您需要更改UI的各个方面时使用。
For your situation you don't really need to have IBOutlets for the buttons because you don't need to change anything about them, you just need to be notified when they have been pressed.
根据您的情况,您不需要为按钮设置IBOutlets,因为您无需更改任何有关它们的信息,只需在按下它们时收到通知即可。
As a note, if you have that many buttons, and you for some reason needed a way to access them from within your code to change something about them i would not recommend using 10 IBOutlets. Instead, i would use the viewWithTag:
method, and set each buttons tag accordingly so that you don't have to go to the trouble of creating IBOutlets for each one.
作为一个注释,如果你有那么多按钮,并且由于某种原因你需要一种方法从你的代码中访问它们来改变它们的一些东西,我不建议使用10个IBOutlets。相反,我会使用viewWithTag:方法,并相应地设置每个按钮标签,这样你就不必为每一个创建IBOutlets而烦恼。
#2
4
An IBOutlet
is a connection to an object, and an IBAction
is a connection to a method to be invoked as a callback. For example, you might have ten buttons and one label, but you might connect to the label using an IBOutlet (so you can update its value), and a single function - (IBAction)buttonPressed:(id)sender;
that is connected to the onTouchUpInside
event of each of the ten buttons. The buttonPressed:
method's implementation will then need to inspect the sender
to figure out what its value is.
IBOutlet是与对象的连接,IBAction是与要作为回调调用的方法的连接。例如,您可能有十个按钮和一个标签,但您可以使用IBOutlet连接到标签(以便更新其值)和单个函数 - (IBAction)buttonPressed:(id)sender;它连接到十个按钮中每个按钮的onTouchUpInside事件。然后,buttonPressed:方法的实现将需要检查发送方以确定其值是什么。
You should not need to have an IBOutlet
for each button if this is all you need to do.
如果您需要这样做,则不需要为每个按钮设置IBOutlet。
#3
0
In your case, I would create one IBOutlet for the label, and one IBAction for the buttons. The IBOutlet for the label would be to update the text when the user pressed a button. Your IBAction would look something like this:
在您的情况下,我将为标签创建一个IBOutlet,为按钮创建一个IBAction。标签的IBOutlet将在用户按下按钮时更新文本。您的IBAction看起来像这样:
-(IBAction)digitPressed:(UIButton *)sender{
//First you have to check if there is currently any text in the label
//If there is not, the below line of code is performed
NSString *textWithDigit = [[yourLabel.text] stringByAppendingString: sender.titleLabel];
//This line updates the label's text
label.text = textWithDigit;
}
I only put in the code relevant to IBActions and IBOutlets (you need an IBOutlet for the label in order to update the text, and you need the IBAction to change the label's text to the digit pressed); there is much more code needed (code needed to check if the user is currently in the middle of typing a number, code for operations like +, -, *, /), but this was just a start.
我只输入与IBActions和IBOutlets相关的代码(为了更新文本,你需要一个标签的IBOutlet,你需要IBAction将标签的文本更改为按下的数字);需要更多的代码(检查用户当前是否正在键入数字,代码为+, - ,*,/等操作的代码),但这只是一个开始。
Hope this helps!
希望这可以帮助!
#1
2
Ok, IBOutlets and IBActions serve two separate purposes.
好的,IBOutlets和IBActions有两个不同的用途。
IBActions are basically methods that can be connected to UI elements through IB. They provide ways for your object to be notified when something has happened with the UI. They also provide the sender argument when they are called so that you can access the UI Element that sent the message from within the method.
IBActions基本上是可以通过IB连接到UI元素的方法。它们提供了在UI发生某些事情时通知对象的方法。它们还在调用时提供sender参数,以便您可以访问从方法中发送消息的UI元素。
An IBOutlet on the other hand offers a way to get a reference to the UI element within your code at any point, it is used when you need to change aspects of the UI.
另一方面,IBOutlet提供了一种在任何时候获取代码中UI元素的引用的方法,它在您需要更改UI的各个方面时使用。
For your situation you don't really need to have IBOutlets for the buttons because you don't need to change anything about them, you just need to be notified when they have been pressed.
根据您的情况,您不需要为按钮设置IBOutlets,因为您无需更改任何有关它们的信息,只需在按下它们时收到通知即可。
As a note, if you have that many buttons, and you for some reason needed a way to access them from within your code to change something about them i would not recommend using 10 IBOutlets. Instead, i would use the viewWithTag:
method, and set each buttons tag accordingly so that you don't have to go to the trouble of creating IBOutlets for each one.
作为一个注释,如果你有那么多按钮,并且由于某种原因你需要一种方法从你的代码中访问它们来改变它们的一些东西,我不建议使用10个IBOutlets。相反,我会使用viewWithTag:方法,并相应地设置每个按钮标签,这样你就不必为每一个创建IBOutlets而烦恼。
#2
4
An IBOutlet
is a connection to an object, and an IBAction
is a connection to a method to be invoked as a callback. For example, you might have ten buttons and one label, but you might connect to the label using an IBOutlet (so you can update its value), and a single function - (IBAction)buttonPressed:(id)sender;
that is connected to the onTouchUpInside
event of each of the ten buttons. The buttonPressed:
method's implementation will then need to inspect the sender
to figure out what its value is.
IBOutlet是与对象的连接,IBAction是与要作为回调调用的方法的连接。例如,您可能有十个按钮和一个标签,但您可以使用IBOutlet连接到标签(以便更新其值)和单个函数 - (IBAction)buttonPressed:(id)sender;它连接到十个按钮中每个按钮的onTouchUpInside事件。然后,buttonPressed:方法的实现将需要检查发送方以确定其值是什么。
You should not need to have an IBOutlet
for each button if this is all you need to do.
如果您需要这样做,则不需要为每个按钮设置IBOutlet。
#3
0
In your case, I would create one IBOutlet for the label, and one IBAction for the buttons. The IBOutlet for the label would be to update the text when the user pressed a button. Your IBAction would look something like this:
在您的情况下,我将为标签创建一个IBOutlet,为按钮创建一个IBAction。标签的IBOutlet将在用户按下按钮时更新文本。您的IBAction看起来像这样:
-(IBAction)digitPressed:(UIButton *)sender{
//First you have to check if there is currently any text in the label
//If there is not, the below line of code is performed
NSString *textWithDigit = [[yourLabel.text] stringByAppendingString: sender.titleLabel];
//This line updates the label's text
label.text = textWithDigit;
}
I only put in the code relevant to IBActions and IBOutlets (you need an IBOutlet for the label in order to update the text, and you need the IBAction to change the label's text to the digit pressed); there is much more code needed (code needed to check if the user is currently in the middle of typing a number, code for operations like +, -, *, /), but this was just a start.
我只输入与IBActions和IBOutlets相关的代码(为了更新文本,你需要一个标签的IBOutlet,你需要IBAction将标签的文本更改为按下的数字);需要更多的代码(检查用户当前是否正在键入数字,代码为+, - ,*,/等操作的代码),但这只是一个开始。
Hope this helps!
希望这可以帮助!