gpt4 book ai didi

ios - UIPageViewController setViewControllers

转载 作者:行者123 更新时间:2023-11-30 11:50:21 27 4
gpt4 key购买 nike

我正在尝试实现一个 PageViewController,当按下按钮时它会切换到不同的 ViewController。我可以在 viewDidLoad() 期间使用 setViewControllers 方法成功加载每个“子”ViewController。

但是,当我在 viewDidLoad() 之外调用 setViewControllers 方法时,它不会更改当前的“子”ViewController。

您可以在 viewDidLoad() 期间随时调用 setViewControllers 方法吗?或者只调用一次?还有一种方法可以更改 pageviewcontrollers 数据源扩展中的变量?

我的项目由一个带有“设置”按钮的条目 ViewController 和一个 ContainerView 组成。在 ContainerView 内部,我有 UIPageViewController。

PageViewController 的代码:

import UIKit

class PageViewController: UIPageViewController {
var slides = [SlideItem]()
var currentIndex: Int!

required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
initSlides()
}

override func viewDidLoad() {
super.viewDidLoad()

if let viewController = viewSlideViewController(currentIndex ?? 0){
let viewControllers = [viewController]

setViewControllers(viewControllers, direction: .forward, animated: true, completion: nil)
}

dataSource = self

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

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func viewSlideViewController(_ index: Int) -> SlideViewController? {

guard let storyboard = storyboard,
let page = storyboard.instantiateViewController(withIdentifier: "SlideViewController") as? SlideViewController else {
return nil
}

page.slide = slides[index]
page.slideIndex = index

return page

}

func viewSettingsViewController(_ index: Int) -> SettingsViewController? {

guard let storyboard = storyboard,
let page = storyboard.instantiateViewController(withIdentifier: "SettingsViewController") as? SettingsViewController else {
return nil
}
return page

}

func initSlides(){

slides.append(SlideItem.init(filename: "Slide 1", comparison: false, highYield: false, videoURL: nil,
groupOrganSystem: ["A"],
groupMedicalSpecialty: ["B"]))
slides.append(SlideItem.init(filename: "Slide 2", comparison: false, highYield: false, videoURL: nil,
groupOrganSystem: ["A"],
groupMedicalSpecialty: ["B"]))
}

func sidebarCommands(button: String, state: Bool){
switch button {
case "settings":
self.setViewControllers([viewSettingsViewController(0)!], direction: .forward, animated: false, completion: nil)
print("settings")
default:
return
}
}

}

扩展:

extension PageViewController : UIPageViewControllerDataSource {

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

if let viewController = viewController as? SlideViewController,
let index = viewController.slideIndex,
index > 0 {
return viewSlideViewController(index - 1)
}
return nil
}

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

if let viewController = viewController as? SlideViewController,
let index = viewController.slideIndex,
(index + 1) < slides.count {
return viewSlideViewController(index + 1)
}
return nil
}
}

enter image description here

最佳答案

关于ios - UIPageViewController setViewControllers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48386289/

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