How to Build A Dynamic UIPageViewController

纵然是瞬间 提交于 2019-12-06 13:41:34

问题


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.

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:

-Instantiate new ViewControllers based on dynamic firebase data that changes each day

-Have each ViewController display the associated image I have stored in Firebase

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]
  }
}

回答1:


After new comments I would do it this way:

  1. Make some Poll object with variables text, images, id, day an so on.

  2. Make an PollViewModel initialized with Poll object, that will be respond to data presentation

  3. Make an UIViewController/UITableViewController subclass initialized with PollViewModel

  4. Make an subclasses of UITableViewCell for every part of the poll UI, that can beand display it in UITableView

  5. Make a PageViewTutorial class (or its own ViewModel) initialized with current array of Poll objects from FireBase

  6. In UIPageViewControllerDataSource handle objects from array and return vc initialized with model initialized with with poll object. ;)



来源:https://stackoverflow.com/questions/36875261/how-to-build-a-dynamic-uipageviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!