PageViewController将数据传递给子控制器

时间:2016-02-24 14:54:19

标签: ios

我有一个页面视图控制器,带有数组引用控制器的故事板ID,我想在里面显示。如何将数据从根控制器传递给控制器​​?

我试过这样做,但它失败了

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

    if let index = pages.indexOf(viewController.restorationIdentifier!) {

        if index < pages.count - 1 {
            return viewcontrollerAtIndex(index + 1)
        }

    }

    var objectController = viewcontrollerAtIndex(0) as! HF2Object
    objectController.object = object
    objectController.serverID = serverID
    objectController.imageURLsArray = imageURLsArray


    return nil
}

整个控制器代码

import UIKit

class HF2PageViewController: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {

var pages = ["object", "reviews"]
var pageViewController = UIPageViewController()
var object:String?
var serverID:String?
var imageURLsArray = [String]()


override func viewDidLoad() {
    super.viewDidLoad()

    pageViewController = UIPageViewController(transitionStyle: .Scroll, navigationOrientation: .Horizontal, options: nil)
    pageViewController.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + 64, self.view.frame.width, self.view.frame.height - 64)

    addChildViewController(pageViewController)
    self.view.addSubview(pageViewController.view)

    pageViewController.delegate = self
    pageViewController.dataSource = self

    pageViewController.setViewControllers([viewcontrollerAtIndex(0)], direction: .Forward, animated: true, completion: nil)
    pageViewController.didMoveToParentViewController(self)


}

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

    if let index = pages.indexOf(viewController.restorationIdentifier!) {

        if index < pages.count - 1 {
            return viewcontrollerAtIndex(index + 1)
        }

    }

    var objectController = viewcontrollerAtIndex(0) as! HF2Object
    objectController.object = object
    objectController.serverID = serverID
    objectController.imageURLsArray = imageURLsArray


    return nil
}

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

    if let index = pages.indexOf(viewController.restorationIdentifier!) {

        if index > 0 {
            return viewcontrollerAtIndex(index - 1)
        }

    }

    return nil
}

func viewcontrollerAtIndex(index: Int) -> UIViewController {
    let vc = storyboard?.instantiateViewControllerWithIdentifier(pages[index])
    return vc!
}

}

1 个答案:

答案 0 :(得分:1)

在您的viewcontrollerAtIndex方法中,您可以将数据传递到vc实例,该实例是您要展示的UIViewController