创建一个视图(从initWithNibName初始化)加载其所有子视图 [英] Make a view (initialized from initWithNibName) load all its subview

查看:214
本文介绍了创建一个视图(从initWithNibName初始化)加载其所有子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我从笔尖加载一个视图控制器,并决定对场景后面的一个子视图做一些事情。在稍后的时刻,我会显示这个视图控制器的视图。

Assuming I load a view controller from a nib, and decide to do something with one of its subview behind the scene. At a later moment in time, I would show the view of this view controller.

viewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil]; 
[viewController.someSubview doSomething];

//later on 
[mainView addSubview:viewController.view];

问题是,在视图出现之前似乎没有加载someSubview对象,所以方法doSomething未被调用。到目前为止,我的解决方法是调用:

The problem is that, the someSubview object doesn't seem to be loaded until the view appears, so the method doSomething is not called. So far my workaround is to call:

[mainView addSubview:viewController.view];
[viewController.view removeFromSuperview];

首先初始化viewcontroller的子视图。是否有更优雅的方式(如loadSubviews方法或其他东西)用于此任务?

to initialize the subviews of viewcontroller first. Is there any more elegant way (like a loadSubviews method or something) for this task?

推荐答案

首先我会尝试简单访问该属性:

First of all I would try simply accessing the property:

[viewController view];

这将强制视图的延迟加载(以及子视图和出口等)和此对你来说可能就足够了。默认情况下,查看控制器在访问之前不会加载视图,因此这可能是您看到延迟的原因。

This will force the lazy load of the view (and thus the subviews and outlets etc.) and this might be enough for you. View controllers by default will not load their view until you access it so this might be why you are seeing a delay.

强制执行此类加载通常是您的解决方案正试图访问一个outlet属性但它实际上还没有被绑定,比如 someSubview 在你的情况下。

Forcing a load like this is usually the solution when you are trying to access an outlet property but it hasn't actually been bound yet, like someSubview in your case.

你也可以通过添加视图但隐藏它来改善一些事情:

You can also improve things a bit by adding the view but making it hidden:

viewController.view.hidden = YES;
[mainView addSubview:viewController.view];

然后在您想要的时候而不是调用 addSubview 要显示它,只需:

Then instead of calling addSubview when you want to show it, just do:

viewController.view.hidden = NO;

我在使用 UIViewAnimationTransitionFlipFromRight进行视图动画时使用了这种方法等过渡。即使强制视图的延迟加载仍然存在明显的延迟,所以我用它来改善性能。

I've used this method when animating in views using the UIViewAnimationTransitionFlipFromRight etc. transitions. Even when forcing a lazy load of the view there is still a noticable lag, so I've used this to improve the performance a bit.

这篇关于创建一个视图(从initWithNibName初始化)加载其所有子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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