gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 07:56:04 26 4
gpt4 key购买 nike

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

这是我在 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 需要其中两个变量,例如参数...所以当 View Controller 出现时,它不起作用...

这是我的 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有用吗?

最佳答案

有几种方法。

最简单的方法是在实例化 vc1、vc2、vc3 时,将变量传递给 View Controller 。

但是,我会根据您的用例构建类。例如,有一个保存变量的数据提供者,这样您的整个页面 VC 流(包括 vc1、vc2、vc3)就可以对数据进行必要的访问。您可以通过子类、委托(delegate)来实现。

关于swift - 如何通过 PageViewController 将 viewController 的数据传递给 pageViewController 的第二个 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48015482/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com