5 Replies
      Latest reply on Aug 15, 2019 11:47 PM by Claude31
      Albinus Level 1 Level 1 (0 points)

        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?

        • Re: UIPageViewController: go to first page
          Claude31 Level 8 Level 8 (7,255 points)

          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")]


              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.

          • Re: UIPageViewController: go to first page
            Albinus Level 1 Level 1 (0 points)

            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.