gpt4 book ai didi

ios - 为什么我的 UIPageViewController 中的页面没有变化?

转载 作者:行者123 更新时间:2023-11-28 12:16:23 24 4
gpt4 key购买 nike

我有一个 UIPageViewController 设置,其中包含我在 Storyboard中创建的 5 个 View Controller 。使用滑动手势可以很好地改变页面,但我希望在用户点击其中一个 View Controller 中的按钮后页面发生变化。这是我的代码:

//  PageVC.swift

import Foundation
import UIKit

class PageVC: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {

lazy var VCArr: [UIViewController] = {
return [self.VCInstance(name: "OB1"),
self.VCInstance(name: "OB2"),
self.VCInstance(name: "OB3"),
self.VCInstance(name: "OB4"),
self.VCInstance(name: "OB5"),
self.VCInstance(name: "OB6")]
}()

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

}

override public func viewDidLoad() {
super.viewDidLoad()
self.dataSource = self
self.delegate = self
if let OB1 = VCArr.first {
setViewControllers([OB1], direction: .forward, animated: true, completion: nil)
}
}

public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?{
guard let viewControllerIndex = VCArr.index(of: 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, viewControllerAfter viewController: UIViewController) -> UIViewController?{
guard let viewControllerIndex = VCArr.index(of: 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 presentationCount(for pageViewController: UIPageViewController) -> Int{

return VCArr.count

}

public func presentationIndex(for pageViewController: UIPageViewController) -> Int{

guard let OB1 = viewControllers?.first,
let OB1Index = VCArr.index(of: OB1) else {
return 0

}

return OB1Index
}

public func nextPageWithIndex(index: Int)
{
let nextVC = VCArr[index]
setViewControllers([nextVC], direction: .forward, animated: true, completion: nil)

}


}

我在其中一个 View Controller 的 IBAction 中调用了 nextPageWithIndex 方法:

//  OB1.swift

import UIKit

class OB1: UIViewController {
@IBOutlet var getStartedColorChange: UIButton!


override func viewDidLoad() {
super.viewDidLoad()



}

@IBAction func getStartedBtn(_ sender: Any) {

let getStarted = PageVC()
getStarted.nextPageWithIndex(index: 2)

}

}

按钮有效,因为我可以从中打印,但页面没有变化。我需要更改什么才能使用按钮更改页面?

最佳答案

问题是您代码中 PageVC() 的新实例:

let getStarted = PageVC()

您必须获取对已创建的父 Controller 的引用!

  @IBAction func getStartedBtn(_ sender: Any) {
let getStarted = self.parent as! PageVC
getStarted.nextPageWithIndex(index: 2)
}

关于ios - 为什么我的 UIPageViewController 中的页面没有变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46392548/

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