UIPageViewController: go to first page

I'm using a UIPageViewController to display various text files. My app uses the Xcode template for a page-based app.


At the moment, when a new file is loaded the controller displays the old text until a page is turned. It then displays the new text, so the data is being loaded properly. What I'd like the controller to do is display the first page of the new file immediately upon loading.


I've defined a notification that's sent when a new text file is loaded. What should my page view controller do when it receives this notification?

Accepted Reply

I found a solution, for the Xcode template code at least. Have the RootViewController respond to a notification posted when a new file is loaded. Then put this code into the function called when the notification is received:


  let startingViewController: DataViewController = self.modelController.viewControllerAtIndex(0, storyboard: self.storyboard!)!
  let viewControllers = [startingViewController]
  self.pageViewController!.setViewControllers(viewControllers, direction: .forward, animated: false, completion: done in })

This code isn't mine: I copied and pasted it from the viewDidLoad function of RootViewController. So far, it works: the first page of the new text is displayed immediately upon being loaded. I just hope I haven't bypassed any important code that will come back to bite me.

Replies

Do you display only one file at a time, with each page in a page of the UIPageViewController ?


Could you show the code of PageViewController ?


You have probably defined orderedViewControllers, for the correct number of pages, something like


    lazy var orderedViewControllers: [UIViewController] = {
        return [self.newVc(viewControllerID: "Page1"),
                self.newVc(viewControllerID: "Page2"),
                self.newVc(viewControllerID: "Page3"),
                self.newVc(viewControllerID: "Page4")]
    }()

with

    func newVc(viewControllerID: String) -> UIViewController {
        return storyboard!.instantiateViewController(withIdentifier: viewControllerID)
    }

Then, on notification, I would call the first page with

            self.setViewControllers([orderedViewControllers[0]], direction: .forward, animated: true, completion: nil)


It would really be useful to see your code to check.

The template code is big and complex, which is why I'm having difficulty seeing just where to intervene. To see it, just create a new Xcode iOS project and select "Page-based app".

I would try to implement in ModelController.


But effectively, it takes some time to fully understand the template app.

I found a solution, for the Xcode template code at least. Have the RootViewController respond to a notification posted when a new file is loaded. Then put this code into the function called when the notification is received:


  let startingViewController: DataViewController = self.modelController.viewControllerAtIndex(0, storyboard: self.storyboard!)!
  let viewControllers = [startingViewController]
  self.pageViewController!.setViewControllers(viewControllers, direction: .forward, animated: false, completion: done in })

This code isn't mine: I copied and pasted it from the viewDidLoad function of RootViewController. So far, it works: the first page of the new text is displayed immediately upon being loaded. I just hope I haven't bypassed any important code that will come back to bite me.

Take care, viewDidLoad is called only once. As you don't show code, it is hard to say if it could happen that you load a new file without reloading Root.