Outlets can be created like this
@interface SearchViewController : UIViewController<UISearchBarDelegate> {
IBOutlet UIView *viewSearchBar;
IBOutlet UIScrollView *scrollVieww;
IBOutlet UILabel *lblName;
}
and also like this
@interface SearchViewController : UIViewController<UISearchBarDelegate> {
}
@property(nonatomic, weak) IBOutlet UIScrollView *scrollVieww;
@property(nonatomic, weak) IBOutlet UIView *viewSearchBar;
@property(nonatomic, weak) IBOutlet UILabel *lblName;
@end
I know the nonatomic
/atomic
strong
/weak
in ARC, but in the first example what are they? strong
, weak
, nonatomic
or atomic
.
Please explain or link me to some detail.
The official answer from Apple is that IBOutlets should be strong. The only case when an IBOutlet should be weak is to avoid a retain cycle. A strong reference cycle can result in memory leaks and app crashes.
It's only after the view controller is initialized that it loads its view. This also means that any outlets declared in the view controller class don't have a value immediately after the view controller's initialization. That's why an outlet is always declared as an (implicitly unwrapped) optional.
@IBOutlet makes Interface Builder recognize the outlet. private ensures the outlet isn't accessed outside the current class. weak is used because in most situations the owner of the outlet isn't the same as the owner of the view. For example, a view controller doesn't own someLabel - the view controller's view does.
An IBOutlet is for hooking up a property to a view when designing your XIB. An IBAction is for hooking a method (action) up to a view when designing your XIB. An IBOutlet lets you reference the view from your controller code.
Instance variables under ARC are strong by default. And they are neither atomic nor nonatomic, since they are just instance variables and not accessor methods. The atomic/nonatomic flags are related to multi threading. They specify whether or not the accessor methods should be atomic. When an accessor is atomic, the execution can't change to an other thread in the middle of the accessor method. When it's nonatomic, there is no such restriction.
Note: IBOutlet is a typedef of nothing. It's just a flag for Interface Builder and has no memory related functions.
Variables are __strong by default under ARC so:
IBOutlet UIView *viewSearchBar;
is the same as
IBOutlet __strong UIView *viewSearchBar;
With regard to the recommended way to deal with IBOutlets under ARC see: the answer to this
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With