self.view = aViewController.view vs [aViewController loadView] -

发布于 2024-08-17 06:39:20 字数 389 浏览 6 评论 0原文

我试图理解从一个视图控制器切换到另一个视图控制器时的行为(显示不同的视图)

addSubiew 语句的一部分似乎有效,我找不到这两个语句发生的情况的解释:

self.view = someViewController.view; [someViewController loadView];

事实上,我遇到的情况是,只有第一个似乎可以工作(显示 someViewController 中定义的视图),而在另一种情况下,只有第二个可以工作。

更准确地说,从根 viewController.view 到 anotherViewController.view (已经实例化)我必须使用第一个,要回来我需要使用第二个。 我不明白当前情况下有什么区别可以让一种或另一种说法发挥作用。

谢谢

I am trying to understand the behavior of view controllers when switching from one to another (displaying different views)

A part form the addSubiew statements which seem to work, I can't find an explanation to what happens with the two statements:

self.view = someViewController.view;
[someViewController loadView];

In fact I got a case where only the first one seems to work (the view defined within someViewController is displayed) and in another case only the second one.

More precisely, from the root viewController.view to anotherViewController.view (already istantiated) I have to use the first one, to come back I need to use the second one.
I can't understand what can it be the difference in the current situation which allows one or the other statement to work.

Thank you

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

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

发布评论

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

评论(2

远山浅 2024-08-24 06:39:20

甚至很难理解你的问题。但我会尝试回答这个问题:

iPhone 操作系统使用一堆视图并向用户显示第一个视图。
您可以将视图弹出(删除)或推送(添加)到此堆栈。

您提供的代码有点“晦涩”。获得控制权的最佳实践是将子视图添加到您的窗口或任何其他视图

,即 [window addSubview:viewController.view];

不要使用 self.view = 覆盖您当前的视图someViewController.view;
相反,初始化新的 ViewController 并将其视图添加到当前视图的子视图中。

有疑问吗?只是评论。

It's very hard to even understand your question. But I'm gonna try to answer it:

The iPhone OS uses a stack of views and display the first one to the users.
You either pop (remove) or push (add) views to this stack.

The code you provided is somewhat "obscure". The best pratice to gain control is to add SubViews to your window or any other view

i.e. [window addSubview:viewController.view];

Don't override your current view by using self.view = someViewController.view;
Instead initialize your new ViewController and add it's view to the subview of your current view.

Any questions? Just comment.

糖果控 2024-08-24 06:39:20

看来您对视图控制器的工作方式有一些误解。

这两个语句有一些问题:

self.view = someViewController.view;

根据 UIViewController.view 的文档:

“每个视图控制器对象都是其视图的唯一所有者。您不得将同一视图对象与多个视图控制器对象关联。” -Apple 文档

一旦该行代码执行,视图将有 2 个不同的控制器,这很糟糕。

下一行:

[someViewController loadView];

这很糟糕,因为您永远不应该显式调用 loadView。

来自 UIViewController.loadView 的文档:

“您永远不应该直接调用此方法。”

来自 UIViewController.view 的文档:

“如果您访问此属性并且其值当前为零,则视图控制器会自动调用 loadView 方法并返回结果视图。”

花一些时间阅读 viewController 教程和指南,例如“View Controller” iPhone 操作系统编程指南”。他们是很好的医生,可以教很多东西。

Looks like you have some misconceptions about how view controllers work.

There are a couple things wrong with those 2 statements:

self.view = someViewController.view;

According to the docs of UIViewController.view :

"Each view controller object is the sole owner of its view. You must not associate the same view object with multiple view controller objects." -Apple Docs

Once that line of code executes, the view would have 2 different controllers, which is bad.

Next line:

[someViewController loadView];

This is bad because you should never explicitly call loadView.

From the docs of UIViewController.loadView :

"You should never call this method directly."

From the docs of UIViewController.view :

"If you access this property and its value is currently nil, the view controller automatically calls the loadView method and returns the resulting view. "

Spend some time with the viewController tutorials and guides such as "View Controller Programming Guide for iPhone OS". They are good docs and can teach a lot.

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