I am writing a small test program using Xcode 5.1.1, for iOS 7.1. I am not using Xib or Storyboard. Everything is done programmatically. In the AppDelegate.m, I create an instance of my TestViewController and set it as the rootViewController of the window. Inside TestViewController.m, I override the "loadView" to create and assign the main view of the controller.
TestViewController.h
--------------------
@interface TestViewController : UIViewController
@property (nonatomic, weak) UILabel *cityLabel ;
@end
TestViewController.m
--------------------
@implementation TestViewController
- (void)loadView
{
UIView *mainView = [[UIView alloc] init] ;
self.view = mainView ;
}
- (void) viewDidLoad
{
UIView *addressView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)] ;
[self.view addSubview:addressView] ;
[self createCityLabel:addressView] ;
}
- (void) createCityLabel:(UIView *)addressView
{
// Warning for below line - Assigning retained object to weak property...
self.cityLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 30)] ;
[addressView addSubview:self.cityLabel] ;
}
@end
As per my understanding, the ownership is as follows
testViewController ---(strong)--> self.view --(strong)--> object of addressView --(strong)--> object of self.cityLabel.
Hence, self.cityLabel can be a weak reference to its target Object
self.cityLabel --(weak)--> object of self.cityLabel.
I did go through some other questions here on similar issues. Everywhere it is suggested to keep the IBOutlet properties inside the ViewController as "weak" (though not mandatory unless there is cyclic reference). Only strong reference maintained is to the main view of the controller.
However, I am getting a warning inside the createCityLabel function as indicated. This goes away if I remove the "weak" attribute. This is really confusing. Is the suggestion to keep Outlets as weak applicable only to those created using Xib/Storyboard?