loadView和viewDidLoad之间有什么区别?

9
我知道这里有一个看似完全相同的问题:iPhone SDK:loadView和viewDidLoad有什么区别?然而,我已经阅读了那个问题,但还是没有得到完整的答案。因为我的UI是动态的,所以我不使用IB。
那么,我应该在loadView中创建self.view,并添加子视图呢,
还是应该在loadView中创建self.view,然后在viewDidLoad中添加子视图呢?
7个回答

25

当您从NIB加载视图并希望在启动后执行进一步的自定义时,请使用viewDidLoad。

如果您想以编程方式创建视图(而不是使用Interface Builder),请使用loadView。


你们每个人都给出了稍微不同的答案,所以我认为没有百分之百确定的答案? - Jonathan.
不,上面的答案是正确的。使用loadView。只有在需要处理通过使用IB构建的NIB文件生成的视图相关内容时,才使用viewDidLoad。 - Cthutu
理论上说,每个都可以在每种情况下工作。然而,我所概述的用法是根据苹果文档的规定。 - RunLoop
不,他的意思是LoadView设置了self.view(它可以来自一个nib文件,但如果找不到nib文件,则设置为空白的新文件),因此如果您覆盖它,则必须自己设置它,尽管不是很复杂,但如果您可以让Apple来完成它,那就应该这样做。 - Jonathan.
loadView仅适用于没有IB的自定义视图。但是,您可以使用具有和不具有IB的viewDidLoad来初始化组件/视图/和其他逻辑。 - Alex Nazarov
显示剩余4条评论

6

针对您的具体问题,您应该在viewDidLoad中添加子视图。因为如果您重写loadView,您必须完成所有工作,加载所有视图。

以下是来自苹果文档的解释:

加载循环中发生的步骤如下:

1.

  * Some part of your application asks for the view in the view

controller’s view property.

2.

  * If the view is not currently in memory, the view controller calls its loadView

method.

3.

  * The loadView method does one of the following:

        If you override this method, your implementation is

responsible for creating all necessary views and assigning a non-nil value to the view property.

        If you do not override this method, the default implementation uses 

the nibName and nibBundle properties of the view controller to try to load the view from the specified nib file. If the specified nib file is not found, it looks for a nib file whose name matches the name of the view controller class and loads that file.

        If no nib file is available, the method creates an empty UIView object 

and assigns it to the view property.

4.

  * The view controller calls its viewDidLoad method to perform any

additional load-time tasks.


3

其实很简单。如果您不使用IB,则您的UIViewController的视图属性为空。所以在loadView中设置它!

我只在loadView中设置视图,没有其他操作。

除此之外,所有操作都在viewDidLoad内完成。以下是一些示例:

- (void)loadView {
    CGRect frame = [[UIScreen mainScreen] applicationFrame];
    baseView = [[UIView alloc] initWithFrame:frame];
    [self setView:baseView];
    [baseView release];
}

就是这样!我完成了。而且永远不想再添加更多。然后在viewDidLoad方法中,我添加了所有我想要的子视图。

- (void)viewDidLoad {
    [super viewDidLoad];

    msg = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, 320, 50)];
    [msg setText:@"Your profile is empty!"];
    [[self view] addSubview:msg]; // hey, I have done my view at loadView, so I have it now
    [msg release];
}

我可能对我的理解有误 :)

1

loadView是实际设置视图的方法(设置所有输出口,包括self.view)。

viewDidLoad可以通过其名称来确定。它是一个委托方法,在视图加载后(所有输出口都已设置)调用,只是通知控制器现在可以开始使用输出口了。

viewDidLoad: “此方法在视图控制器将其关联的视图加载到内存中后调用。无论视图是存储在nib文件中还是在loadView方法中以编程方式创建,都会调用此方法。”

loadView: “如果您手动创建视图,则必须重写此方法并使用它来创建视图。”


0

只有在想要自己创建视图时才使用loadView。

在使用界面构建器或使用nib初始化后,不要再使用loadView,因为这些操作已经在底层实现中调用了loadView。

此外,在使用loadView时,请先分配视图,然后再进行任何其他设置:

    -(void)loadView {
       [super loadView];
       // if you do any things here before assigning a view
       // it will try to get a view first by calling loadView()
       // and ends up with a crash since a dead loop.          
       self.view = ...;//assign your view here
       //do other settings
    }

0
在viewDidLoad中添加子视图。这样你就可以100%确定视图确实已经加载并准备好使用了。

0

使用 viewDidLoad 初始化视图和控件。如果没有 Nib/Xib 并且希望您的 ViewController 有自定义(非 UIView)视图,则使用 loadView


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接