我应该在viewDidLoad或viewWillLayoutSubviews上初始化viewController吗 [英] Should I initialize my viewController on viewDidLoad or viewWillLayoutSubviews

查看:277
本文介绍了我应该在viewDidLoad或viewWillLayoutSubviews上初始化viewController吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到有时在viewDidLoad上我得到正确的视图大小.有时候我不知道.

I noticed that sometimes on viewDidLoad I got the view size right. Sometimes I don't.

例如

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.pullToRefreshController = [self.pullToRefreshController initWithDelegate:self];
    PO(self.view);
    PO(self.view.superview);
    PO(self.view.superview.superview);
    PO(self.view.superview.superview.superview);

    while(false);
}
-(void)viewWillLayoutSubviews
{
    PO(self.view);
    PO(self.view.superview);
    PO(self.view.superview.superview);
    PO(self.view.superview.superview.superview);
    while (false);
}

在viewDidLoad中,self.view的大小仍为320到480.在viewWillLayoutSubviews中已修复.

at viewDidLoad the size of self.view is still 320 to 480. At viewWillLayoutSubviews that have been fixed.

我想知道在什么之间以及应该在哪里初始化东西?还是应该在viewDidLoad中添加什么东西,在viewWillLayoutSubviews中添加什么东西?

I wonder what happen in between and where should I initialize stuffs? Or what stuffs should be in viewDidLoad and what stuffs should be in viewWillLayoutSubviews?

推荐答案

viewDidLoad是创建和初始化要添加到主视图的子视图的好地方.这也是进一步自定义主视图的好地方.这也是初始化数据结构的好地方,因为在调用此属性时,应该已经在视图控制器上设置了任何属性.通常只需一次.

viewDidLoad is a good place to create and initialize subviews you wish to add to your main view. It is also a good place to further customize your main view. It's also a good place to initialize data structures because any properties should have been set on the view controller by the time this is called. This typically only needs to be done once.

viewWillLayoutSubviews是放置和布置子视图(如果需要)的位置.在旋转或其他事件导致调整视图控制器的视图大小之后,将调用此方法.在视图控制器的生命周期中,这可能发生很多次.您只应在此处布置视图.

viewWillLayoutSubviews is where you position and layout the subviews if needed. This will be called after rotations or other events results in the view controller's view being sized. This can happen many times in the lifetime of the view controller. You should only layout the views here.

这篇关于我应该在viewDidLoad或viewWillLayoutSubviews上初始化viewController吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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