Marc Lamberti
Marc Lamberti

Reputation: 821

When should I use an IBOutlet instead of an IBAction?

I know the differences between IBAction and IBOutlet, but I don't know when I should use one over the other.

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?

Upvotes: 0

Views: 758

Answers (3)

Chance Hudson
Chance Hudson

Reputation: 2859

Ok, IBOutlets and IBActions serve two separate purposes.

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.

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.

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.

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.

Upvotes: 2

pasawaya
pasawaya

Reputation: 11595

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:

-(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.

Hope this helps!

Upvotes: 0

davidgoli
davidgoli

Reputation: 2495

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.

You should not need to have an IBOutlet for each button if this is all you need to do.

Upvotes: 4

Related Questions