loadView:UIView iOS中的功能 [英] loadView: functions in UIView iOS

查看:84
本文介绍了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子类
  • 接下来,我创建了一个没有xib的UIViewController子类.
  • 最后,在第三步中创建的类的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:仅在view属性为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
  1. Why is this iPhone program not calling -loadView?
  2. loadView

希望这会有所帮助.

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

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