当我将数据从TableView传递给子PageViewController时,它可以转到下一个子节点

时间:2017-01-21 01:52:58

标签: ios swift uiviewcontroller xcode7 uipageviewcontroller

我有UIViewController UITableView。然后我创建一个PageViewController,其中包含两个viewcontroller。如果我将数据传递给子级,则Pageview不起作用。如果我改变

setViewControllers([subjective], direction: .Forward, animated: true, completion: nil)

setViewControllers([firstVC], direction: .Forward, animated: true, completion: nil)

网页浏览工作但数据未通过。我对此一无所知。请帮帮我。感谢

代码工作:

Pageview控制器类

import UIKit

class PageVC : UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {

var lblhoten = String()
var lblngaysinh = String()
var lblsodt = String()

lazy var VCArr: [UIViewController] = {
    return [self.VCInstance("ThongTinBNPage2"),
            self.VCInstance("ThongTinBNPage3")]
}()

private func VCInstance(name: String) -> UIViewController {
    return UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier(name)
}

override func viewDidLoad() {
    super.viewDidLoad()
    self.dataSource = self
    self.delegate = self
    if let firstVC = VCArr.first {
        let subjective = self.storyboard?.instantiateViewControllerWithIdentifier("ThongTinBNPage2") as! VCSubjective
        subjective.lblhoten = lblhoten
        subjective.lblngaysinh = lblngaysinh
        subjective.lblsodt = lblsodt
        setViewControllers([subjective], direction: .Forward, animated: true, completion: nil)
    }
}

public func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
    guard let viewcontrollerindex = VCArr.indexOf(viewController) else {
        return nil
    }

    let previousindex = viewcontrollerindex - 1


    guard previousindex >= 0 else {
        return VCArr.last
    }

    guard VCArr.count > previousindex else {
        return nil
    }

    return VCArr[previousindex]
}

public func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
    guard let viewcontrollerindex = VCArr.indexOf(viewController) else {
        return nil
    }

    let nextindex = viewcontrollerindex + 1


    guard nextindex < VCArr.count else {
        return VCArr.first
    }

    guard VCArr.count > nextindex else {
        return nil
    }

    return VCArr[nextindex]
}

public func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
    return VCArr.count
}

// The selected item reflected in the page indicator.
public func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
    guard let firstviewcontroller = viewControllers?.first,
        let firstviewcontrollerindex = VCArr.indexOf(firstviewcontroller) else {
            return 0
    }

    return firstviewcontrollerindex
}

}

VCSubjective:

import UIKit

class VCSubjective: UIViewController {

@IBOutlet var hoten: UILabel!
@IBOutlet var ngaysinh: UILabel!
@IBOutlet var sodt: UILabel!

var lblhoten = String()
var lblngaysinh = String()
var lblsodt = String()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.        
    hoten.text = lblhoten
    ngaysinh.text = lblngaysinh
    sodt.text = lblsodt
}

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


}

1 个答案:

答案 0 :(得分:1)

这里的问题是你引用了两个完全不同的对象。

viewDidLoad中创建ThongTinBNPage2 viewController,然后将其添加到pageViewController的viewControllers属性中。但是,存储在VCArr中的对象是两个完全不同的viewControllers。

让我们这样思考:

  1. 调用viewDidLoad时,您需要创建viewController object #1
  2. 然后将viewController object #1分配给pageViewController的viewControllers对象,使其值为viewControllers = [object #1]
  3. pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController?pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?中,您引用了VCArr对象。在第一次调用VCArr时,它懒洋洋地创建了两个完全不同的视图控制器对象[object #2, object #3]
  4. 要修复此代码,您需要执行以下操作:

    if let firstVC = VCArr.first {
        let subjective = firstVC   // DO NOT create different VC here
        subjective.lblhoten = lblhoten
        subjective.lblngaysinh = lblngaysinh
        subjective.lblsodt = lblsodt
        setViewControllers([subjective], direction: .Forward, animated: true, completion: nil)
    }