简体   繁体   English

如何在UIPageViewController中的两个ViewController之间传递数据

[英]How to pass data between two ViewController in UIPageViewController

I have two UIViewControllers, A and B, I connect them within a UIPageViewController: 我有两个UIViewControllers A和B,我将它们连接在UIPageViewController中:

Here is how it looks in the Storyboard: 这是在情节提要中的外观: 图片

I don't know how to pass data to B from A. 我不知道如何将数据从A传递到B。

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. 现在,所有3个视图控制器共享相同的模型。 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. 最干净的可能是使用委托,该委托将向页面控制器报告,然后向给定的视图控制器报告。

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: 在发送数据的ViewController中,如下定义一个委托:

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: 在PageViewController中,如下设置您的View Controller:

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: 最后,接收方的ViewController如下实现委托:

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 亚伦

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用委托将数据从 UIPageViewController 传递给子 ViewController - how to pass data from UIPageViewController to child ViewController using delegates 如何在ViewController和子视图ViewController之间传递数据 - How to pass data between a ViewController and a subview ViewController 如何使用闭包在两个 ViewController 之间传递数据 - How to pass data between two ViewController using closure 如何在UIPageViewController中的UIViewControllers之间传递数据并保存变量 - How should I pass data between UIViewControllers in UIPageViewController and save variables 更新UIPageViewController的ViewController中的数据 - Update data in ViewController of UIPageViewController 如何在Swift中在SplashScreen和ViewController之间传递数据 - How to pass data between SplashScreen and ViewController in Swift 如何在PageViewController和ViewController之间传递数据? - How to pass data between PageViewController and ViewController? 如何将ViewController添加到UIPageViewController - How to add ViewController to UIPageViewController 在UIPageViewController中的不同滑动视图之间传递数据 - Pass data in UIPageViewController between different swipe views iOS swift prepareForSegue无法在两个ViewController之间传递数据 - iOS swift prepareForSegue cannot pass data between two viewcontroller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM