Amir_P Amir_P - 11 months ago 153
Swift Question

PageViewController showing a black screen

In storyboard I have a

UIPageViewController
and this is my code

class ReservedTimesViewController: UIPageViewController {

override func viewDidLoad() {
super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}

extension ReservedTimesViewController: UIPageViewControllerDataSource {
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ReservedTimesTableViewController")
print(controller)
return controller
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ReservedTimesTableViewController")
print(controller)
return controller
}


and this is my
ReservedTimesTableViewController
in story board
enter image description here

but I'm getting a black screen. Why it's not working correct?

Answer Source

On loading your controller you have to set your initial page by using this code

override func viewDidLoad() {
    super.viewDidLoad()
    dataSource = self;
    self.setViewControllers([getViewControllerAtIndex(index: 0)] as [UIViewController], direction: UIPageViewControllerNavigationDirection.forward, animated: false, completion: nil)
}

func getViewControllerAtIndex(index: Int) -> ReservedTimesTableViewController
{
    // Create a new view controller and pass suitable data.
    let reservedTimesTableViewController = self.storyboard?.instantiateViewController(withIdentifier: "ReservedTimesTableViewController") as! ReservedTimesTableViewController
    return reservedTimesTableViewController
}

After when you try to scroll your page then the datasource methods will do their work.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download