切换 UIViewControlles - 模棱两可

发布于 2025-01-07 04:50:57 字数 1125 浏览 1 评论 0原文

我已经阅读了很多有关此问题的问题,但仍然找不到此问题的正确解决方案。我有一个新闻应用程序,它在启动屏幕上启动(以获取新闻提要并缓存它们),然后切换到主屏幕,这是新闻文章的混搭。您可以通过单击文章、部分、画廊、视频等从该屏幕导航到许多其他屏幕...我有一个固定的页眉和页脚,我决定对主 UIViewController 进行子类化并将其放置在其上(我'我将在其他 UIViewController 中继承此 UIViewController)

我读到,执行此操作的最佳方法是使用多个视图控制器,每个视图控制器控制应用程序的特定部分。然而,我仍然对如何正确且负责任地在这些之间进行切换感到有点困惑。我尝试了故事板转场,但遇到了两个问题:

  1. 我需要创建很多转场,因为你几乎可以从 任何屏幕到任何其他屏幕
  2. 一旦您跳转到另一个屏幕,segues 将不会保留 UIViewController 的状态,一旦您返回,它就会重置为它的状态 初始状态。

我还读过有关presentModalViewController 的内容,您可以使用它将视图作为模式推送到当前控制器之上。然而,您需要经常这样做,而且我有点担心内存消耗问题。

创建没有应用程序视图层次结构的多视图应用程序的最佳方法是什么(UINavigationController/UITabController/等...)通常我想在开始时声明所有视图控制器,然后根据需要在它们之间切换(当我首先需要一个视图,我将初始化它)之前有人尝试过类似的东西吗?

多谢。

编辑:感谢您的回复,我出去观看了有关 UIViewController 遏制的 WwDC 会议,这帮助我了解如何实现自定义 UIViewController 容器。然而,我还是有一些无法理解的地方。

当我创建自定义 UIViewController 容器时,我需要使用 addChildViewController 将子项添加到其中,这将触发我的子 UIViewController 上的 willMoveToParentController 事件。

然后,我使用 [self.view addSubview...] 将子控制器子视图添加到容器视图中,但这只会加载视图,不会将它们链接到我在子 UIViewController 中创建的事件。

我应该使用 addSubview 之外的其他东西吗?或者我应该使用单独的 UIView 类来连接我的事件?

I've read a lot of questions regarding this issue but still I couldn't find a proper solution to this problem. I have a news application that starts on a Splash Screen (To fetch the news feeds and cache them) then it switches to the main screen which is a mash-up of news articles. You can navigate from this screen to many other screens by clicking on the articles, sections, galleries, videos, etc...I have a fixed header and a footer which I decided to subclass a master UIViewController and place those on that (I'll inherit this UIViewController in my other UIViewControllers)

I've read that the best way to do this is by having multiple view controllers each controlling a specific section of my application. However I'm still a bit confused about how to switch between those properly and responsibly. I tried the storyboard segues but I ran into two problems:

  1. I need to create a lot of segues since you can virtually jump from
    any screen to any other screen
  2. The segues will not preserve the state of the UIViewController once you jump to another one and once you go back it's reset to it's
    initial state.

I have also read about the presentModalViewController with which you can push your view as a modal on top of the current controller. However you need to do this a lot and I'm a bit afraid about memory consumption issues.

What's the best way to create a multi-view application without an application view hiearchy (UINavigationController/UITabController/etc...) Typically I'd like to declare all my view controllers at the start and just switch between them as needed (when I first need a view I'll initialize it) Anyone tried something similar before?

Thanks a lot.

EDIT: Thanks for the replies, I went out and watched WwDC session about UIViewController containment and that helped me in understanding how to implement a custom UIViewController container. However there's still something I can't grasp.

When I create a custom UIViewController container I need to add children to it using addChildViewController, and that will fire the willMoveToParentController event on my child UIViewController.

Then I add the child controllers subviews into the container views using [self.view addSubview...] however this only load the views and doesn't link them to the events I have created inside my child UIViewController.

Should I use something else than addSubview? or should I use a separate UIView class to wire my events in?

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

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

发布评论

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

评论(1

雨后彩虹 2025-01-14 04:50:58

如果您使用新的 iOS 5,我建议您阅读 UIViewController 类参考

通过这种方式,您可以创建一种管理其他视图控制器的容器控制器。关键是您可以实现自己的控制器和切换动画。

此模式可以应用于故事板和非故事板应用程序。我更喜欢第二种方法。

关于内存消耗,如果您以正确的方式管理内容,则不必担心。也许,如果您已经缓存了数据,您可以从视图层次结构中添加或删除当前显示的控制器的 view (但将控制器保留为主控制器的子级)或删除两者。

希望有帮助。

If you use the new iOS 5 I suggest you to read about Implementing a Container View Controller section in UIViewController Class Reference.

In this manner you can create a sort of container controller that manages your other view controllers. The key aspect is that you can implement your own controller and switching animations.

This pattern can be applied both to storyboard and non-storyboard application. I prefer the second approach.

About memory consumption, you have not to worry if you have managed content in the right way. Maybe, if you have already cached the data, you can add or remove the current displayed controller's view from the view hiearchy (but leaving the controller as a child of your master controller) or remove both.

Hope it helps.

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