[Solved] When I pass data from TableView to child PageViewController, it can go to next child


The problem here is that you are referencing two completely different objects.

In viewDidLoad you create ThongTinBNPage2 viewController and then add it to the viewControllers property of the pageViewController. However, the objects stored in VCArr are two totally different viewControllers.

Let’s think about it this way:

  1. When viewDidLoad is called you create viewController object #1
  2. Then you assign the viewController object #1 to the viewControllers object of the pageViewController making the value of viewControllers = [object #1]
  3. In pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? and pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? you reference the VCArr object. On the first call to VCArr, it lazily creates two completely different view controller objects [object #2, object #3]

To fix this code you need to do the following:

if let firstVC = VCArr.first {
    let subjective = firstVC   // DO NOT create different VC here
    subjective.lblhoten = lblhoten
    subjective.lblngaysinh = lblngaysinh
    subjective.lblsodt = lblsodt
    setViewControllers([subjective], direction: .Forward, animated: true, completion: nil)
}

1

solved When I pass data from TableView to child PageViewController, it can go to next child