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

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

问题描述

例如:

  1. 使用 initWithNibName 创建一个新的 UIVC,使用nib-v1"
  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 *)name owner:(id)owner options:(NSDictionary *)options 的新笔尖加载视图code> 方法并自己进行视图交换.

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