Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring IBOutlet inside or outside @interface?

sorry If am I being too picky on this one but I am learning iOS programming now and I've seem some people who declare the IBOutlet like this:

IBOutlet attached to property

#import <UIKit/UIKit.h>
#import "CustomCell.h"

@interface CustomTableViewController : UITableViewController {
  CustomCell *customCell;
}
@property (nonatomic, retain) IBOutlet CustomCell *customCell;
@end

And some declaring like this:

IBOutlet attached to the declaration inside the interface

#import <UIKit/UIKit.h>
#import "CustomCell.h"

@interface CustomTableViewController : UITableViewController {
  IBOutlet CustomCell *customCell;
}
@property (nonatomic, retain) CustomCell *customCell;
@end

which one is the proper way to declare it? Are any differences between them? If someone know to explain why do they put it on different places it would be awesome to learn.

Thanks a lot :)

like image 847
zanona Avatar asked Jan 08 '11 17:01

zanona


People also ask

How do you declare IBOutlet property?

If you have a property defined that you want to make accessible to your storyboards, just add the @IBOutlet attribute before your property. Similarly with @IBAction to connect storyboard actions back to code. class MyViewController: UIViewController { @IBOutlet weak var likeButton: UIButton?

What is IBOutlet and IBAction?

@IBAction is similar to @IBOutlet , but goes the other way: @IBOutlet is a way of connecting code to storyboard layouts, and @IBAction is a way of making storyboard layouts trigger code. This method takes one parameter, called sender . It's of type UIButton because we know that's what will be calling the method.

What is IBAction Objective C?

IBAction – a special method triggered by user-interface objects. Interface Builder recognizes them. @interface Controller { IBOutlet id textField; // links to TextField UI object } - (IBAction)doAction:(id)sender; // e.g. called when button pushed.


1 Answers

Both of those are still "inside the interface" so your title it a bit confusing but I see what you are asking.

In many cases the result of either approach will be the same but they are different. An IBOutlet property will call the property's setter method which gives you an opportunity to override that setter if setting that property should have some side effect.

I prefer to use outlets on properties because I think it makes the memory management of the objects loaded from the nib much clearer. Take a look at memory management of nib objects and I think you will see what I mean.

Objects in the nib file are created with a retain count of 1 and then autoreleased. As it rebuilds the object hierarchy, UIKit reestablishes connections between the objects using setValue:forKey:, which uses the available setter method or retains the object by default if no setter method is available. This means that (assuming you follow the pattern shown in “Outlets”) any object for which you have an outlet remains valid. If there are any top-level objects you do not store in outlets, however, you must retain either the array returned by the loadNibNamed:owner:options: method or the objects inside the array to prevent those objects from being released prematurely.

IBOutlet ivars will call setters for those ivars if they exists and directly retain the object loaded from the nib if no setter is found.

Advertising the property as the IBOutlet at least makes it clear that the property's setter will always be used and follow whatever memory management rule has been set for that property.

Finally I argue that IBOutlets are part of the public interface of a class and it is therefore better to expose methods (via a property) for working with them eager than using -setValue:forKey: to manipulate the backing ivars which should be an implementation detail.

like image 68
Jonah Avatar answered Sep 23 '22 08:09

Jonah