如何构建动态UIPageViewController [英] How to Build A Dynamic UIPageViewController
问题描述
我已按照基本教程此处创建一个UIPageViewController,用于浏览不同的ViewControllers,我在Main.Storyboard上创建了所有这些。我想创建一个生成动态ViewControllers的应用程序,这意味着我必须以编程方式在UIPageViewController中实例化ViewControllers。
I have followed the basic tutorial here to create a UIPageViewController that swipes through different ViewControllers, all of which I create on Main.Storyboard. I want to make an application that generates dynamic ViewControllers, meaning I will have to instantiate the ViewControllers in the UIPageViewController programatically.
然而,我试图将所有内容都进一步。我将根据Firebase中的数据显示一组动态的ViewControllers。在每次滑动时,我想实例化一个包含图像的视图(图像将存储在Amazon Web Services上,并且图像的链接将存储在Firebase中)。我目前在我的Android应用程序中进行了此设置,并使用Picasso在我的片段中加载图像。
However, I am trying to take everything a bit further. I am going to have a dynamic set of ViewControllers to display based on data I have in Firebase. On each swipe, I want to instantiate a view that is going to contain an image (the image will be stored on Amazon Web Services, and a link to the image will be stored in Firebase). I currently have this setup in my Android application and use Picasso to load the image in my fragment.
由于所有数据都是动态的,我想找到最好的方法:
Since all of the data is going to be dynamic, I want to find the best way to:
- 根据每天都在变化的动态firebase数据来实例化新的ViewControllers
-Instantiate new ViewControllers based on dynamic firebase data that changes each day
-Have每个ViewController显示我存储在Firebase中的相关图像
-Have each ViewController display the associated image I have stored in Firebase
所有ViewControllers都将具有相同的骨架,这意味着每个ViewControllers都会有一个带有一些文本的图像。以下是我目前的代码,我希望得到一些建议/帮助。
All of the ViewControllers will have the same "skeleton," meaning each one will have an image accompanied by some text. Below is my current code, and I was hoping to receive some advice / assistance.
import UIKit
class PageViewTutorial: UIPageViewController {
override func viewDidLoad() {
super.viewDidLoad()
dataSource = self
if let firstViewController = orderedViewControllers.first {
setViewControllers([firstViewController],
direction: .Forward,
animated: true,
completion: nil)
}
}
//this will be dynamic based on firebase data
private(set) lazy var orderedViewControllers: [UIViewController] = {
return [self.newColoredViewController("Green"),
self.newColoredViewController("Red"),
self.newColoredViewController("Blue")]
}()
private func newColoredViewController(color: String) -> UIViewController {
return UIStoryboard(name: "Main", bundle: nil) .
instantiateViewControllerWithIdentifier("\(color)ViewController")
}
}
// MARK: PageViewTutorialDataSource
extension PageViewTutorial: UIPageViewControllerDataSource {
func pageViewController(pageViewController: UIPageViewController,
viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.indexOf(viewController) else {
return nil
}
let previousIndex = viewControllerIndex - 1
guard previousIndex >= 0 else {
return nil
}
guard orderedViewControllers.count > previousIndex else {
return nil
}
return orderedViewControllers[previousIndex]
}
func pageViewController(pageViewController: UIPageViewController,
viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.indexOf(viewController) else {
return nil
}
let nextIndex = viewControllerIndex + 1
let orderedViewControllersCount = orderedViewControllers.count
guard orderedViewControllersCount != nextIndex else {
return nil
}
guard orderedViewControllersCount > nextIndex else {
return nil
}
return orderedViewControllers[nextIndex]
}
}
推荐答案
在发表新评论后我会这样做:
After new comments I would do it this way:
-
使用变量text,images,id,day等来制作一些Poll对象。
Make some Poll object with variables text, images, id, day an so on.
初始化PollViewModel使用Poll对象,将响应数据显示
Make an PollViewModel initialized with Poll object, that will be respond to data presentation
使用PollViewModel初始化UIViewController / UITableViewController子类
Make an UIViewController/UITableViewController subclass initialized with PollViewModel
为poll UI的每个部分创建一个UITableViewCell的子类,可以在UITableView中显示它
Make an subclasses of UITableViewCell for every part of the poll UI, that can beand display it in UITableView
创建一个PageViewTutorial类(或它自己的ViewModel)用来自FireBase的当前Poll对象数组初始化
Make a PageViewTutorial class (or its own ViewModel) initialized with current array of Poll objects from FireBase
在UIPageViewControllerDataSource中处理来自数组的对象并使用poll obje初始化模型返回vc克拉。 ;)
In UIPageViewControllerDataSource handle objects from array and return vc initialized with model initialized with with poll object. ;)
这篇关于如何构建动态UIPageViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!