Which storage (weak or strong) for the elements underneath the "main" View in iOS7

780 views Asked by At

After reading really many books and docs on iOS programming I am still confused - which Storage type (weak or strong) to select, when control dragging from a UI element underneath the "main" View? (and by "main" I think I mean the UIView assigned to the view property of the ViewController):

enter image description here

For example I'm trying to recreate the example iOS SDK: Working with NSUserDefaults by Mr. Jeroen van Rijn - with Xcode 5.0.2 and iOS7.

enter image description here

His code is lacking the weak/strong specifiers and I wonder, which would be correct here?

#import <UIKit/UIKit.h>
@interface ContactViewController : UIViewController {
    IBOutlet UIImageView *contactImageView;
    IBOutlet UITextField *firstNameTextField;
    IBOutlet UITextField *lastNameTextField;
    IBOutlet UITextField *ageTextField;
}
- (IBAction)save:(id)sender;
- (IBAction)chooseImage:(id)sender;
@end
2

There are 2 answers

1
Antonio MG On BEST ANSWER

Because that view is already retained by the view that creates them (the one you set in the interface builder) you don't need to retain them again, so you have to choose weak, not strong.

0
Greg On

You don't need outlet for the view in this example. You can access it like:

self.view

in your ContactViewController or like:

ContactViewController *contact = [[ContactViewController alloc] init];
contact.view

from other view controller. However for your outlets you should you weak.