1

im new in this and Im trying to pass some variables for a viewController through a pageViewController who manages 3 viewControllers and use those variables in the 3 controllers, can anybody help me?

here is my code on swift...

I send my variables with this:

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!

when I do this, my viewController who makes a call to a web service needs two of those variables like parameter... so when the view controller appear, doesn't work...

this is my RootPageViewController class

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.
}



}

the variables are passed directly to this class, I print them and they are displayed when I enter the PageViewController, but how do I pass them from this to the first viewController (principal1) of the PageViewController so that the call to the webservice works?

Kevin Arvizu
  • 135
  • 1
  • 1
  • 7

1 Answers1

0

There are several ways.

Easiest way is when you instantiate the vc1, vc2, vc3, pass the variables into the view controllers.

However, I would structure the classes based on your use-cases. For ex, have a data provider that holds your variables, so your entire page VC flow (including vc1, vc2, vc3) can have necessary access to the data. You can achieve that by subclasses, delegates.

Dillon
  • 364
  • 5
  • 18