通过控制器无法按预期方式传递数据

时间:2019-06-19 21:45:50

标签: uipageviewcontroller uistoryboardsegue swift4.2

有人可以告诉我为什么我的代码无法按我预期的那样工作吗?我想将值从ViewController传递到FVC控制器。我认为问题出在页面控制器(“测试”)上。

这是我的故事板的SS: enter image description here

这是我的代码

class ViewController: UIViewController {
var a = "abcdefg"
var test : TEST!
override func viewDidLoad() {
    super.viewDidLoad()
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "first" {
        if segue.destination.isKind(of: TEST.self) {
            test = segue.destination as! TEST
            test.b = a
        }
    }
}


}

还有PageController的代码


    var b = ""
    lazy var subViewControllers:[UIViewController] = {
        return [
            UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "FVC") as! FVC,UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SVC") as! SVC,UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "TVC") as! TVC
        ]
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        dataSource = self
        delegate = self
        setViewControllerFromIndex(index: 0)
    }

    func setViewControllerFromIndex(index:Int) {
        self.setViewControllers([subViewControllers[index]], direction: UIPageViewController.NavigationDirection.forward, animated: true, completion: nil)
    }
    }

extension TEST: UIPageViewControllerDelegate, UIPageViewControllerDataSource {
    func presentationCount(for pageViewController: UIPageViewController) -> Int {
        return subViewControllers.count
}

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        let currentIndex:Int = subViewControllers.index(of: viewController) ?? 0
        if currentIndex <= 0 {
            return nil
        }
        return subViewControllers[currentIndex-1]
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        let currentIndex:Int = subViewControllers.index(of: viewController) ?? 0
        if currentIndex >= subViewControllers.count-1 {
            return nil
        }
        return subViewControllers[currentIndex+1]
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "FVC" {
            let vc = segue.destination as? FVC
            vc?.g = b
        }
    }
```}


And third  Controller 

``` class FVC: UIViewController {

    var g = ""

    override func viewDidLoad() {
        super.viewDidLoad()

        test()
    }

    func test(){
        var s = "aaaaa"  + g
        print(s)

   }

因此应为“ aaaaaabcdefg”,但应为“ aaaaa”。如果有人告诉我使用这种情况传递数据的好方法,我将不胜感激。

0 个答案:

没有答案