你可以将NIB文件换成已经在屏幕上的UIViewController吗? [英] Can you swap the NIB file for a UIViewController that's already on-screen?

查看:137
本文介绍了你可以将NIB文件换成已经在屏幕上的UIViewController吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:


  1. 使用nib-v1创建一个使用initWithNibName的新UIVC

  2. 显示它,例如使用[(UINavigationController)nav pushViewController:myVC]

  3. 将myVC使用的NIB更改为nib-v2

据我所知,这是许多应用程序的正确应用程序设计方法,在分析信息时,您需要两个稍微不同的UI屏幕才能显示信息。

So far as I can see, this is the "correct" approach to app-design for a lot of apps, when paging through information where you need two slightly different UI screens for the info being displayed.

例如,您的大多数页面都是文本,但其中一些页面也有图像(想想RSS阅读器,其中一些RSS条目有文本+图像,有些是仅限文本)。

For instance, most of your pages are text, but some of them have an image too (think of an RSS reader, where some RSS entries have text + image, some are text only).

我之前已经通过一个NIB文件和第二个不可见的名为UIView的实例处理了这个问题,我在第一个文件的顶部分层,并根据上下文打开/关闭,使用隐藏标志。

I've dealt with this previously by having one NIB file with a second, invisible, named UIView instance that I layered over the top of the first one, and switched on/off depending on on context, using the "hidden" flag.

但这显然是错误的,浪费了内存。

But this is clearly wrong, and wastes memory.

但是,我看不到从NIB文件重新加载视图的明显方法。我猜我想以某种方式重现initWithNibName所做的魔法?

However, I cannot see an obvious way to "reload" the view from the NIB file. I'm guessing I want to somehow reproduce the magic that initWithNibName does?

我怀疑这是可能的,但我确信如果你这样做错误的方式然后应用程序就会崩溃。

I suspect this is possible, but I'm sure that if you do it "the wrong way" then the app will simply crash horribly.

推荐答案

我同意Rob,但如果你真的想要交换笔尖(这很糟糕,因为它很容易导致悬挂指针等),您可以使用 NSBundle 从新笔尖加载视图 - (NSArray *)loadNibNamed:(NSString *)名称所有者:(id)所有者选项:(NSDictionary *)选项方法并自己进行视图交换。

I agree with Rob, but if you really want to mess with swapping nib's (which is bad as it can easily lead to dangling pointers and the like), you could maybe load the view from the new nib with NSBundle's - (NSArray *)loadNibNamed:(NSString *)name owner:(id)owner options:(NSDictionary *)options method and do the view swapping yourself.

您应该为不同类型的内容使用不同的视图控制器。如果它们只是略有不同,您仍然可以考虑创建一个基类并为不同的变体创建子类。

You should rather use different view controllers for different types of content. If they only differ slightly, you can still consider creating one base class and subclassing the different variations.

这篇关于你可以将NIB文件换成已经在屏幕上的UIViewController吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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