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

查看:64
本文介绍了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添加init代码时,我最终得到了无限的堆栈跟踪,所以我通常在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添加init代码时,我最终会得到无限的堆栈跟踪

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

不读self.view in -loadView。只有设置它,不要获取它。

自我。如果当前未加载视图,则查看属性访问者调用 -loadView。这是你的无限递归。

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

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

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天全站免登陆