user3054705
user3054705

Reputation: 21

Cannot pass data from UIViewController to UIPageViewController and other views

I have a segue pointing to a PageViewControler, who controls two ViewControlers and whileI think I have it setup correctly and I can run it smoothly, I cannot pass data to the other two views. What Im I missing?
Anyway, this is my segue pointing from a cell to a PageViewControler:

   class HomeViewController:  UIViewController, UITableViewDelegate, UITableViewDataSource {
     if segue.identifier == "showDetails" {

            let localDetails = segue.destination as! localDetails

            let IndexPath = self.tableView.indexPathForSelectedRow!
            let row = IndexPath.row

        tableView.deselectRow(at: IndexPath, animated: true)

            localDetails.index = row
            let singleLocalInfo = restaurants[row]
            localDetails.localName = singleLocalInfo.locale }}

My PageViewControler:

class PageViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {

   func newVc(viewController: String) -> UIViewController {
         return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: viewController)
     }

    lazy var orderedViewControllers: [UIViewController] = {
         return [self.newVc(viewController: "localDetails"),
                 self.newVc(viewController: "blueScreen")]
     }()  etc,etc... }

And finally the viewControler where I need data to be diplayed:

class localDetails: UIViewController {

  var localName: String = ""

  etc... 

}

I had to cut the most of the code but yo get the idea. Thanks

Upvotes: 0

Views: 43

Answers (1)

DonMag
DonMag

Reputation: 77423

Basically...

In your class that instantiates and segues to your page view controller, set the properties there, then set them in their respective controllers.

Assuming "showDetails" is the segue identifier for your page view controller:

class HomeViewController:  UIViewController, UITableViewDelegate, UITableViewDataSource {

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        if segue.identifier == "showDetails" {

            let pageVC = segue.destination as! PageViewController

            let IndexPath = self.tableView.indexPathForSelectedRow!
            let row = IndexPath.row

            tableView.deselectRow(at: IndexPath, animated: true)

            pageVC.index = row
            let singleLocalInfo = restaurants[row]
            pageVC.localName = singleLocalInfo.locale
            pageVC.blueValue = "the value for blueScreen"

        }

    }
}

class PageViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {

    var index: Int = 0
    var localName: String = ""
    var blueValue: String = ""

    func newVc(viewController: String) -> UIViewController {
        return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: viewController)
    }

    lazy var orderedViewControllers: [UIViewController] = {
        let v1 = self.newVc(viewController: "localDetails")
        v1.localName = self.localName
        let v2 = self.newVc(viewController: "blueScreen")
        v2.blueValue = self.blueValue
        return [v1, v2]
    }()

    //etc,etc...

}

Upvotes: 1

Related Questions