如何将数据从UIPageViewController传递到其“子视图”之一?

时间:2018-07-02 02:39:05

标签: ios swift xcode uipageviewcontroller

好的,这是我的问题:

我想通过多个视图控制器将string类型的变量的值传递到UIPageViewController的页面之一中。这是UIPageViewController的代码(从下面的导入UIKit开始):

import UIKit

class PageViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {

    var recieveProdTime2 = String()     // 2nd view controller for productive time data to pass through

    lazy var orderedViewControllers: [UIViewController] = {
        return [self.newVC(viewController: "sbHome"), self.newVC(viewController: "sbPoints"), self.newVC(viewController: "sbShop")]
    }()

    var pageControl = UIPageControl()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.dataSource = self
        if let firstViewController = orderedViewControllers.first {
            setViewControllers([firstViewController],
                               direction: .forward,
                               animated: true,
                               completion: nil)
        }

        self.delegate = self
        configurePageControl()

        if recieveProdTime2 != "" {
            performSegue(withIdentifier: "segue3", sender: self)
        }

    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let destinationVC = segue.destination as! HomeViewController
        destinationVC.productive = recieveProdTime2
    }

    func configurePageControl() {

        pageControl = UIPageControl(frame: CGRect(x: 0, y: UIScreen.main.bounds.maxY - 50, width: UIScreen.main.bounds.width, height: 50))
        pageControl.numberOfPages = orderedViewControllers.count
        pageControl.currentPage = 0
        self.view.addSubview(pageControl)

    }

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

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {

        guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
            return nil
        }

        let previousIndex = viewControllerIndex - 1

        guard previousIndex >= 0 else {
            return orderedViewControllers.last
        }

        guard orderedViewControllers.count > previousIndex else {
            return nil
        }

        return orderedViewControllers[previousIndex]

    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {

        guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
            return nil
        }

        let nextIndex = viewControllerIndex + 1

        guard orderedViewControllers.count != nextIndex else {
            return orderedViewControllers.first
        }

        guard orderedViewControllers.count > nextIndex else {
            return nil
        }

        return orderedViewControllers[nextIndex]

    }

    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {

        let pageContentViewController = pageViewController.viewControllers![0]
        self.pageControl.currentPage = orderedViewControllers.index(of: pageContentViewController)!
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

如您所见,我正在实例化3种不同的视图。我想将recieveProdTime2的值传递给HomeViewController,它是UIPageViewController中的视图之一。这是HomeViewController的代码:

import UIKit

class HomeViewController: UIViewController {

    @IBOutlet weak var focusSession: UILabel!
    @IBOutlet weak var breakSession: UILabel!

    var productive = String()   // Productive time data from ViewController.swift arrives and is store

    override func viewDidLoad() {
        super.viewDidLoad()

        focusSession.text = productive

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

我在PageViewController类中使用“ prepare(for segue:...)”方法,并调用HomeViewController类中名为“ productive”的变量来设置该类中名为“ focusSession”的标签的文本。

问题

一切正常运行,没有错误,并且项目得以构建并运行....但是标签文本为空!...它只是在模拟器中显示为空标签!我选中了错误框,然后看到以下消息:“警告:尝试在其视图不在窗口层次结构中的Focus.PageViewController上显示Focus.HomeViewController!”

如何将数据从PageViewController发送到HomeViewController并正确设置标签文本?

1 个答案:

答案 0 :(得分:0)

尝试一下:

将您的orderedViewControllers初始化替换为:

lazy var orderedViewControllers: [UIViewController] = {
    let sbPoint = self.newVC(viewController: "sbPoints") as! HomeViewController
    if recieveProdTime2 != "" {
        sbPoint.productive = recieveProdTime2
    }
    return [self.newVC(viewController: "sbHome"), sbPoint , self.newVC(viewController: "sbShop")]
}()

然后在recieveProdTime2中输入一些值。 在这里,您无需使用prepareForSegue,因此请删除:

if recieveProdTime2 != "" {
    performSegue(withIdentifier: "segue3", sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let destinationVC = segue.destination as! HomeViewController
    destinationVC.productive = recieveProdTime2
}