如何最好地将使用一个 UIViewController 将所有页面作为 UIView 处理的图书应用程序转换为 StoryBoarding 和 UIPageView?
我有一个图书应用程序,大约有 30 页,每个页面都是一个 UIViewController 中的 UIView。我想将其转换为使用 StoryBoarding 和 UIPageViews 的应用程序。看起来 UIPageView 中的每个“页面”都必须是 UIViewController,而不是 UIView。解决这个问题的最佳方法是什么?我需要为每个 UIView 页面制作一个 UIViewController 吗?
I have a book app that has about 30 pages that are each a UIView in a single UIViewController. I want to transition this to an app that uses StoryBoarding and UIPageViews. It looks like each of the "pages" in UIPageView must be a UIViewController, not a UIView. What would be the best way of going about this? Do I need to make a UIViewController out of each UIView page?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的。将所有
UIView
子类重写为UIViewController
子类。我已经在我的几个项目中做到了这一点,结果发现这有点乏味。对于我的项目,我发现最简单的方法是创建一个新的 Xcode 项目并构建新的基础设施。然后为每个旧的UIView
类创建新的UIViewController
类。然后,我将旧UIView
类中的大部分代码复制并粘贴到新的UIViewController
类中。我将很多self
更改为self.view
。UIViewController
的生命周期比 UIView 稍微复杂一些,因此我最终将一些初始化代码从指定的初始化器移至viewDidLoad
和viewWillAppear<我的 UIViewController 子类中的 /code> 方法。
对评论的回应:在我的应用程序中,许多页面都有独特的交互功能,因此我编写了一个独特的 UIViewController 子类来管理这些功能。有些页面将具有相同的功能但内容不同。这些只会使用一个
UIViewController
子类的多个实例。我的模型对象将创建这些 UIViewController 子类并用适当的内容填充它们。换句话说,无需为每个页面编写新的
UIViewController
子类。页面上只有尽可能多的具有独特功能的数量。如果您的页面不执行任何操作,您甚至可以只使用UIViewController
而无需子类化。Yep. Rewrite all your
UIView
subclasses asUIViewController
subclasses. I've done it with a few of my projects, and it turns out to be just a little tedious. For my projects I found it easiest to just create a new Xcode project and build the new infrastructure. Then create newUIViewController
classes for each of my oldUIView
classes. Then I copied and pasted most of the code from the oldUIView
classes into newUIViewController
classes. I changed a lot ofself
toself.view
. The life cycle of aUIViewController
is a bit more complex than a UIView, so I ended up moving some initialization code from the designated initializer to theviewDidLoad
andviewWillAppear
methods in myUIViewController
subclasses.Response to comment: In my apps many pages have unique interactive features, so I write a unique
UIViewController
subclass to manage those features. Some pages will have the same features but different content. These will just use multiple instances of oneUIViewController
subclass. My model object will make theseUIViewController
subclasses and populate them with the appropriate content.In other words, there's no need to write a new
UIViewController
subclass for every page. Only as many as you have unique functionality on a page. If your page doesn't do anything, you could even just useUIViewController
without subclassing.请查看iOS 5 iPhone UIPageViewController 应用程序示例一文。该章将简要概述页面视图控制器背后的概念以及旨在演示此类实际操作的示例应用程序。
Check out An Example iOS 5 iPhone UIPageViewController Application article. That chapter will provide a brief overview of the concepts behind the page view controller and an example application designed to demonstrate this class in action.