以编程方式创建视图强Vs控制器中的弱子视图 [英] Creating views programmatically Strong Vs Weak subviews in controller

查看:95
本文介绍了以编程方式创建视图强Vs控制器中的弱子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Xcode 5.1.1为iOS 7.1编写一个小测试程序。我没有使用Xib或Storyboard。一切都是以编程方式完成的。在AppDelegate.m中,我创建了一个TestViewController的实例,并将其设置为窗口的rootViewController。在TestViewController.m中,我重写loadView来创建和分配控制器的主视图。

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 ---(强) - > self.view - (强) - > addressView对象 - (强) - > self.cityLabel的对象。

testViewController ---(strong)--> self.view --(strong)--> object of addressView --(strong)--> object of self.cityLabel.

因此,self.cityLabel可以是对其目标对象的弱引用

Hence, self.cityLabel can be a weak reference to its target Object

self.cityLabel - (弱) - > self.cityLabel的对象。

self.cityLabel --(weak)--> object of self.cityLabel.

我在这里遇到过类似问题的其他一些问题。建议将ViewOtroller中的IBOutlet属性保持为弱(尽管不是强制性的,除非有循环引用)。只有强引用才能保持控制器的主视图。

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.

但是,我在createCityLabel函数中收到警告,如图所示。如果我删除弱属性,这就消失了。这真令人困惑。是否建议将Outlets保持为弱适用于仅使用Xib / Storyboard创建的那些?

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?

推荐答案

您的 cityLabel 属性可能很弱,但您必须先将其添加到视图层次结构中,然后才能分配属性或将其分配给标准(强引用)变量。

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.

发生了什么事情,你正在创建一个 UILabel ,然后将它分配给一个不承认它的所有权(弱)。在您超过 self.cityLabel = [[UILabel alloc] ... 行之后, UILabel 已经已被解除分配且 cityLabel 属性为零。

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];

变量 theLabel 将保留 UILabel createCityLabel:的范围内,并添加 UILabel 作为作为View Controller视图一部分的视图的子视图将在视图控制器的生命周期内保留它(除非您从视图或任何<$中删除 UILabel c $ c> UILabel 的父视图))。

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)).

这篇关于以编程方式创建视图强Vs控制器中的弱子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆