How to Make Uipageviewcontroller Reuse Controller Like Tableview Reuse Cell

Checking approach: Managing UIPageViewController page data using UITableView cells

For this interested in the solution I've posted to GitHub at:
https://github.com/gallaugher/PageViewControllerDemo
I've posted with more generic names - PageVewController, DetailViewController, and ListViewController, so this is easier to reuse and perhaps follow.

Thanks to Sazan Dauti for answering this question outside of StackOverflow. It is possible to segue directly between the PageController & the ListController that contains a TableView for managing pages an array of similar pages (e.g. like Apple does in the Weather app where you can add/delete/move cities), keeping the Detail (Cities in original example) free of any knowledge that it's in a PageView.
- Drag a segue directly from the UIPageViewController to the UIViewController with the TableView that contains, in my case, a list of locations (not the detail that's managed by the PageView). The segue should present modally. Key variables should be passed to the ListViewController in a prepare for segue in the PageViewController like this:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ToListViewController" {
let controller = segue.destination as! ListViewController
controller.listArray = listArray
controller.currentPage = currentPage
}
}
  • Be sure the values passed (listArray & currentPage in the case above) are declared in ListViewController.

  • Add an Unwind method to the PageViewController similar to this:

    @IBAction func unwindFromListViewController(sender: UIStoryboardSegue) {
    pageControl.numberOfPages = listArray.count
    pageControl.currentPage = currentPage
    setViewControllers([createDetailViewController(currentPage)], direction: .forward, animated: false, completion: nil)
    }

setViewControllers above refers to the function written in the PageViewController that contains the logic to instantiate the DetailViewController for the current page.

  • Return segue is created by dragging from a TableViewCell in the ListView to the "Exit' button (far right of the three buttons at the top/title view of this UIViewController). You'll be asked the name for an unwind method that should be in the PageViewController (in the example above I've called it unwindFromListViewController), so be sure to add this method before trying to make the segue.

  • Add a prepare for segue function to the ListViewController:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "ToPageViewController" {
    let controller = segue.destination as! PageViewController
    controller.currentPage = currentPage
    controller.listArray = listArray
    }
    }

  • and in tableView didSelectRowAt, trigger the perform segue:

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    currentPage = indexPath.row // set current page to row selected
    performSegue(withIdentifier: "ToPageViewController", sender: self)
    }

I couldn't find any decent examples online demonstrating how this was done, so hopefully this is understandable and efficient. Any corrections are welcome. Cheers!

UIpageViewController And Timer

First crate a Timer with timeInterval and option repeats seat to true.

Example:

Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(scrollToNextItem), userInfo: nil, repeats: true)

Now you have Timer that will trigger scrollToNextItem method every 3 seconds.

Now read about setViewControllers(_:direction:animated:completion:) to know how to set view controllers to be displayed.

Than implement scrollToNextItem method with required logic.

Create a UIPageViewController with a UITableView in each page

I was able to solve my own problem, and it seems I was just a bit mixed up.

The comment from rdelmar set me on the right track but I hooked the delegate and data source to the wrong object. I had to connect them to File's Owner in order for it to work.

In addition it seems theTableView was not necessary and when I removed that my code suddenly worked as expected.

If this isn't clear enough for an answer please tell me how I can be more specific. Thank you!



Related Topics



Leave a reply



Submit