How to pass data between two ViewController in UIPageViewController

末鹿安然 提交于 2019-12-13 05:01:15

问题


I have two UIViewControllers, A and B, I connect them within a UIPageViewController:

Here is how it looks in the Storyboard:

I don't know how to pass data to B from A.


回答1:


Well assume you have some class (which you should have provided) like:

class MyModel {
    var dataFromFirstController: Any?
    var dataFromSecondController: Any?
    var sharedData: Any?
}

Now you need a subclass of page view controller which is the one that controls the data so override view did load to create a model:

var myModel: MyModel!

override func viewDidLoad() {
    super.viewDidLoad()
    self.myModel = MyModel()
}

Now when you generate or fetch view controllers you simply assign the same model to them:

func getFirstViewController() -> UIViewController {
    let controller = MyFirstController.generate()
    controller.myModel = self.myModel
    return controller
}

func getSecondViewController() -> UIViewController {
    let controller = MySecondController.generate()
    controller.myModel = self.myModel
    return controller
}

Now all 3 view controllers share the same model. This is probably the easiest way of doing it but there are very many ways. The cleanest is probably using delegates which would report back to page controller that would then report back to given view controllers.




回答2:


I had some difficulty finding a solution to this and came up with something myself using delegation. Suggestions are welcome

In the ViewController sending the data, define a delegate as follows:

protocol FirstVCDelegate {  
    func foo(someData: String)  
}  

class FirstViewController: UIViewController {  
    var delegate: FirstVCDelegate?  

    ....  

    func someMethod() {  
        delegate?.foo("first VC")  
    }  

}  

In the PageViewController set up your View Controllers as follows:

class PageViewController: UIPageViewController, ... {  
    var myViewControllers = [UIViewController]()  

override func viewDidLoad() {  
    let firstVC = storyboard?.instantiateViewController(withIdentifier: "FirstViewController") as! FirstViewController  
    let secondVC = storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController  

    firstVC.delegate = secondVC  

     myViewControllers.append(firstVC)  
     myViewControllers.append(secondVC)  

}  

// MARK: - PageVC Delegate / Datasource   
....

and finally, the receiving ViewController implements the delegate as follows:

class SecondViewController: UIViewController, FirstVCDelegate {  

....  

    func foo(data: String) { // This method is triggered from FirstViewController's delegate?.foo("first VC")  
        print(data)  // "first VC" will be printed  
    }  
}  

Good luck,

Aaron



来源:https://stackoverflow.com/questions/50694888/how-to-pass-data-between-two-viewcontroller-in-uipageviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!