iPhone SDK:loadView 和 viewDidLoad 有什么区别? [英] iPhone SDK: what is the difference between loadView and viewDidLoad?

查看:27
本文介绍了iPhone SDK:loadView 和 viewDidLoad 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 iPhone 应用程序中使用视图和视图控制器时,谁能解释一下 loadView 和 viewDidLoad 之间的区别?

When working with views and view controllers in an iPhone app, can anyone explain the difference between loadView and viewDidLoad?

我的个人情况是,我从代码构建我的所有视图,我不会也不会使用 Interface Builder,如果这有什么不同的话.

My personal context, is that I build all my views from code, I do not and will not use Interface Builder, should that make any difference.

我发现当我向 loadView 添加初始化代码时,我最终会得到一个无限的堆栈跟踪,所以我通常在 viewDidLoad 中构建我所有的子视图......但是我真的不清楚什么时候每个得到执行,以及放置 init 代码更合适的位置.最完美的是初始化调用的简单图表.

I've found that often when I add init code to loadView, I end up with an infinite stack trace, so I typically do all my child-view building in viewDidLoad...but it's really unclear to me when each gets executed, and what is the more appropriate place to put init code. What would be perfect, is a simple diagram of the initialization calls.

谢谢!

推荐答案

我可以猜出这里可能存在的问题,因为我已经做到了:

I can guess what might be the problem here, because I've done it:

我发现当我向 loadView 添加初始化代码时,我经常会得到一个无限的堆栈跟踪

I've found that often when I add init code to loadView, I end up with an infinite stack trace

不要在-loadView中读取self.view.设置它,不要获取它.

如果视图当前未加载,则 self.view 属性访问器调用 -loadView.这是你的无限递归.

The self.view property accessor calls -loadView if the view isn't currently loaded. There's your infinite recursion.

在 -loadView 中以编程方式构建视图的常用方法,如 Apple 的 pre-Interface-Builder 示例中所示,更像是这样:

The usual way to build the view programmatically in -loadView, as demonstrated in Apple's pre-Interface-Builder examples, is more like this:

UIView *view = [[UIView alloc] init...];
...
[view addSubview:whatever];
[view addSubview:whatever2];
...
self.view = view;
[view release];

我不怪你不使用IB.我在 Instapaper 的所有内容中都坚持使用这种方法,并且发现与处理 IB 的复杂性、界面怪癖和意想不到的幕后行为相比,我更适应它.

And I don't blame you for not using IB. I've stuck with this method for all of Instapaper and find myself much more comfortable with it than dealing with IB's complexities, interface quirks, and unexpected behind-the-scenes behavior.

这篇关于iPhone SDK:loadView 和 viewDidLoad 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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