سؤال

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?

هل كانت مفيدة؟

المحلول

Your cityLabel property can be weak, but you must add it to the view hierarchy before you can assign the property or assign it to a standard (strong reference) variable.

What is going on is that you're creating a UILabel, then assigning it to a property which assumes no ownership of it (weak). After you've gone past the self.cityLabel = [[UILabel alloc] ... line, the UILabel has already been deallocated and the cityLabel property is nil.

This will correctly do what you intend:

UILabel *theLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 80.0f, 30.0f)];
self.cityLabel = theLabel;
[addressView addSubview:theLabel];

The variable theLabel will retain the UILabel during for scope of createCityLabel: and adding the UILabel as a subview to a view that is part of the View Controller's view will retain it for the life of the view controller (unless you remove the UILabel from the view or any of the UILabel's parent views)).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top