loadView:UIView iOS 中的函数 [英] loadView: functions in UIView iOS

查看:18
本文介绍了loadView:UIView iOS 中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白loadView:函数的机制(这个函数在UIView中).

I don't understand the mechanism of loadView: function (this function is in UIView).

我创建了一个项目如下:

I created a project as below:

  • 首先,我创建了一个 iPhone 的基于窗口的项目.
  • 然后,我创建了一个 UIView 子类
  • 接下来,我创建了一个 UIViewController 子类,没有 xib.
  • 最后,在我第三步创建的类的loadView:函数中,指定UIView对象(在我第二步创建的类中)作为UIViewController 对象(在第三步中).
  • First, I created a iPhone's window-based project.
  • Then, I created a UIView subclass
  • Next, I created a UIViewController subclass, with no xib.
  • Lastly, in the loadView: function of the class I created in the third step, I designate the UIView object (in the class I created in the second step) as the view variable of the UIViewController object (in the third step).

如果省略最后一步,将语句NSLog(@"test LoadView");放在loadView:函数中,那么当项目运行时,语句NSLog(@"test LoadView"); 连续调用,导致运行溢出.

If I omit the last step, and place the statement NSLog(@"test LoadView"); in the loadView: function, then when the project is run, the statement NSLog(@"test LoadView"); is invoked continuously, result in the run is overflow.

请解释一下!谢谢!

推荐答案

loadView: 仅在视图属性为 nil 时调用.以编程方式创建视图时使用此选项.default: 创建一个没有子视图的 UIView 对象.对于前 -

loadView: is only invoked when the view property is nil. Use this when creating views programmatically. default: create a UIView object with no subviews. For ex -

- (void)loadView 
{ 
    UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame]; 
    [view setBackgroundColor:color]; 
    self.view = view; 
    [view release]; 
}

通过实现 loadView: 方法,您可以连接到默认的内存管理行为.如果内存不足,视图控制器可能会收到 didReceiveMemoryWarning 消息.默认实现会检查视图是否在使用中.如果它的视图不在视图层次结构中并且视图控制器实现了 loadView: 方法,它的视图将被释放.稍后当需要视图时,再次调用 loadView: 方法来创建视图.

By implementing the loadView: method, you hook into the default memory management behavior. If memory is low, a view controller may receive the didReceiveMemoryWarning message. The default implementation checks to see if the view is in use. If its view is not in the view hierarchy and the view controller implements the loadView: method, its view is released. Later when the view is needed, the loadView: method is invoked again to create the view.

不确定为什么要使用 loadView: 但你可以在 viewDidLoad:

Not sure why you want to use loadView: but you can do just as much in viewDidLoad:

参考 -

  1. 为什么这个 iPhone 程序没有调用 -loadView?
  2. loadView

希望这会有所帮助.

这篇关于loadView:UIView iOS 中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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