gpt4 book ai didi

ios - Swift - 单击按钮更改 UIPageViewController View

转载 作者:行者123 更新时间:2023-11-28 10:31:08 25 4
gpt4 key购买 nike

我正在使用 UIPageViewController,我的目的是在其 UIViewController 之一中单击 UIButton 时切换当前 View 。我已经用 google 搜索了我的问题并找到了一些类似的线索,但我无法确定答案。

例如,我的第二个 UIViewController vc2 中的按钮应将 View 更改为 vc3

RootPageViewController

import UIKit

class RootPageViewController: UIPageViewController, UIPageViewControllerDataSource {

lazy var viewControllerList:[UIViewController] = {

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

let vc1 = sb.instantiateViewController(withIdentifier: "timelineView")
let vc2 = sb.instantiateViewController(withIdentifier: "mainView")
let vc3 = sb.instantiateViewController(withIdentifier: "addView")

return [vc1, vc2, vc3]
}()

override func viewDidLoad() {
super.viewDidLoad()

self.dataSource = self

let secondViewController = viewControllerList[1]
self.setViewControllers([secondViewController], direction: .forward, animated: false, completion: nil)

}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let vcIndex = viewControllerList.firstIndex(of: viewController) else { return nil }

let previousIndex = vcIndex - 1
guard previousIndex >= 0 else { return nil }

guard viewControllerList.count > previousIndex else { return nil }

return viewControllerList[previousIndex]
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let vcIndex = viewControllerList.firstIndex(of: viewController) else { return nil }

let nextIndex = vcIndex + 1

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

guard viewControllerList.count > nextIndex else { return nil }

return viewControllerList[nextIndex]
}

}


第二个 ViewController 内的按钮

@IBAction func addData(_ sender: Any) {


}

最佳答案

RootPageViewControllerviewDidLoad 中,将第二个 View Controller 按钮的目标添加到 self 并将 addData 方法添加到 RootPageViewController。您可以使用 setViewControllers 并移动到该方法中的最后一个 View Controller

override func viewDidLoad() {
super.viewDidLoad()

self.dataSource = self
if let secondViewController = viewControllerList[1] as? SecondViewController {
self.setViewControllers([secondViewController], direction: .forward, animated: false, completion: nil)
secondViewController.button.action = #selector(addData(_:))
}
}
@IBAction func addData(_ sender: Any) {
if let thirdViewController = viewControllerList.last {
self.setViewControllers([thirdViewController], direction: .forward, animated: false, completion: nil)
}
}

在 MainViewControlle 中保留 addData 方法,并像这样向其添加协议(protocol)

protocol MainVCDelegate {
func buttonPlusTapped()
}
class MainViewController: UIViewController {

@IBOutlet var buttonPlus: UIBarButtonItem!

var delegate: MainVCDelegate?
@IBAction func addData(_ sender: Any) {
delegate?.buttonPlusTapped()
}
}

在 RootPageViewController 中确认这个委托(delegate)并添加委托(delegate)方法

class RootPageViewController: UIPageViewController, UIPageViewControllerDataSource, MainVCDelegate {

func buttonPlusTapped() {
if let thidViewController = viewControllerList.last {
self.setViewControllers([thidViewController], direction: .forward, animated: false, completion: nil)
}
}

lazy var viewControllerList:[UIViewController] = {

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

let vc1 = sb.instantiateViewController(withIdentifier: "timelineView")
let vc2 = sb.instantiateViewController(withIdentifier: "mainView") as! MainViewController
vc2.delegate = self
let vc3 = sb.instantiateViewController(withIdentifier: "addView")
return [vc1, vc2, vc3]
}()
}

关于ios - Swift - 单击按钮更改 UIPageViewController View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55847188/

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