何时使用多个笔尖?

发布于 2024-11-17 11:34:59 字数 354 浏览 3 评论 0原文

假设我的应用程序有 6 个窗口,1 个窗口是主窗口,其他 5 个窗口用于设置、分数、统计等。

那么,如果我是正确的,那么由于内存管理而使用了多个笔尖? 因此,如果我将所有 6 个视图放入一个 nib 文件中,则在加载应用程序时,它将同时加载所有 6 个视图并占用大量内存,但如果我对 6 个视图使用 6 个 nib,则在启动时应用程序将仅加载第一个视图,当我单击示例“选项”时,它将加载 Options.nib 并显示视图。

是否有更多理由使用多个笔尖而不是在一个笔尖中使用多个视图?

我说得对吗? 我怎么知道何时使用 addSubview 或presentModalViewController? 当我使用这两种方法切换视图时,主要区别是什么?

Let's say my application has 6 windows, 1 windows is main windows, and other 5 are for settings, scores, statistics etc.

So if I'm correct, multiple nibs are used because of memory managament?
So If I put all 6 views in one nib file, when loading application it will load all 6 views at the same time and take a lot of memory, but if I use 6 nibs for 6 views, on startup application will load only first view, and when I click in exammple "Options" then it will load Options.nib and diplay the view.

Are there more reasons to use multiple nibs instead of multiple views in one nib?

Am I right?
And how do I know when to use addSubview or presentModalViewController?
What is the main difference when I'm using those two methods to switch views?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不爱素颜 2024-11-24 11:34:59
  1. 关于记忆,你是对的。笔尖的所有视图同时加载到内存中。您可能不需要立即使用它们。所以在这种情况下你就是在浪费内存。如果您有很多视图,这甚至可能会使应用程序崩溃。

  2. 添加单独的笔尖通常会导致代码更加结构化。设置和分数之间没有逻辑关系。所以你应该为它们设置单独的类和笔尖。如果你不这样做,你可能会得到难以管理的代码,这将需要更多的时间来维护和修改。

  1. You are right about memory. All the views of a nib are loaded simultaneously in memory. You may not need them at once. So in that case you are wasting memory. This can even crash the app if you have many views.

  2. Adding separate nibs in general lead to more structured code. There is no logical relation between settings and score. So you should have separate classes and nibs for them. If you don't do that, you may have an unmanageable code which will require more time in maintaining and modifying.

挽梦忆笙歌 2024-11-24 11:34:59

Nib(或 XIB)文件与内存管理几乎无关。它们的存在是为了方便。与手动编码所有自动调整大小蒙版和操作相比,使用可视化工具布局 UI 并连接操作更容易。但是,您是对的,您不想立即将所有视图加载到内存中,因为这会造成浪费

使用多个 XIB 文件对于保持项目更加结构化很有用。 (在某些情况下,您甚至可能为单个视图控制器提供备用笔尖。尽管常见情况是 1:1 的比例。)

addSubviewpresentModalViewController 方法确实不同。 addSubview 获取一个视图并将其添加到给定视图(或将其置于最前面)。这意味着您将负责保留视图控制器,以免发生奇怪的事情。这将用于在主视图中呈现其他视图,例如在横幅等中呈现消息。

相比之下,presentModalViewController 用于在短时间内呈现单个视图,例如登录表单或信息屏幕。您逐步传入整个视图控制器,并且通常会在不久之后释放视图控制器。事实上,这样的代码很常见:

SomeViewControllerSubclass *myVC = [[SomeViewControllerSubclass alloc] initWithNibName:@"SomeViewControllerSubclass" bundle:nil];
[self presentViewController:myVC];
[myVC];

注意我们如何在呈现 SomeViewControllerSubclass 实例后立即释放它。您不能使用 addSubview 来做到这一点,因为这样您可能会留下一个没有控制器的视图。你必须自己保留它。

Nib (or XIB) files have almost nothing to do with memory management. They are there for convenience purposes. It's easier to lay out UI and wire up actions with a visual tool than hand coding all of the autoresizing masks and actions. However, you are correct, you don't want to load all of your views into memory at once as that would be a waste.

Using multiple XIB files is useful for keeping your project more structured. (You might even have alternate nibs for a single view controller in some cases. Although the common case is a 1:1 ratio.)

The addSubview and presentModalViewController methods are really different. addSubview takes a view and adds it to a given view (or brings it to the forefront). This means that you would be responsible for retaining the view controller so that odd things don't happen. This would be used for presenting other views in your main view, such as presenting a message in a banner or the like.

By contrast, presentModalViewController is meant for presenting a single view for a short amount of time, such as a log in form, or an informational screen. You pass in the entire view controller piecemeal and often will release the view controller shortly thereafter. In fact, it's common to see code like this:

SomeViewControllerSubclass *myVC = [[SomeViewControllerSubclass alloc] initWithNibName:@"SomeViewControllerSubclass" bundle:nil];
[self presentViewController:myVC];
[myVC];

Notice how we release the instance of SomeViewControllerSubclass immediately after presenting it. You can't do that with addSubview, because then you may be left with a view without it's controller. You have to retain it yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文