如何通过PageViewController将viewController的数据传递给pageViewController的第二个ViewController

时间:2017-12-28 23:05:52

标签: swift xcode

我是新的,我试图通过一个管理3个viewControllers并在3个控制器中使用这些变量的pageViewController为viewController传递一些变量,有人可以帮助我吗?

这是我在swift上的代码......

我发送变量:

public var lenguaje = ""
public var username = ""
public var indice = ""
public var password = ""

  if segue.identifier == "loginPage" {
        let destino = segue.destination as! RootPageViewController
        destino.lenguaje=lenguaje
        destino.username = self.txtUsuario.text!
        destino.indice = indice
        destino.password = self.txtPassword.text!

当我这样做时,调用Web服务的viewController需要其中两个变量,比如参数...所以当视图控制器出现时,它不起作用......

这是我的RootPageViewController类

class RootPageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {

public var lenguaje = ""
public var username = ""
public var indice = ""
public var password = ""

lazy var viewControllerList: [UIViewController] = {

    let sb = UIStoryboard(name: "Main" , bundle: nil)

    let vc1 = sb.instantiateViewController(withIdentifier: "principal1") //this is well done
    let vc2 = sb.instantiateViewController(withIdentifier: "Principal2")
    let vc3 = sb.instantiateViewController(withIdentifier: "Principal3")

    return [vc1,vc2,vc3]


}()

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

    guard let vcIndex = viewControllerList.index(of: viewController) else { return nil}

    let previo = vcIndex - 1

    guard previo >= 0 else {return nil}

    guard viewControllerList.count > previo else {return nil}

    return viewControllerList[previo]

}

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

    guard let vcIndex  = viewControllerList.index(of: viewController) else {return nil}

    let next = vcIndex + 1

    guard viewControllerList.count != next else {return nil}

    guard viewControllerList.count > next else {return nil}

    return viewControllerList[next]
}


override func viewDidLoad() {
    super.viewDidLoad()

    self.dataSource = self

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



    // Do any additional setup after loading the view.
}



}

变量直接传递给这个类,我打印它们并在我进入PageViewController时显示它们,但是我如何将它们传递给PageViewController的第一个viewController(principal1)以便调用webservice作品?

1 个答案:

答案 0 :(得分:0)

有几种方法。

最简单的方法是在实例化vc1,vc2,vc3时,将变量传递给视图控制器。

但是,我会根据您的用例构建类。例如,有一个保存变量的数据提供程序,因此整个页面VC流(包括vc1,vc2,vc3)可以访问数据。您可以通过子类,委托实现这一目标。