UIPageViewController setViewControllers

时间:2018-01-22 16:37:04

标签: ios swift uipageviewcontroller

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

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

你可以在viewDidLoad()中随时调用setViewControllers方法或只调用一次吗?还有办法在pageviewcontrollers dataSource扩展中更改变量吗?

我的项目包含一个带有“设置”按钮和ContainerView的条目ViewController。在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

3 个答案:

答案 0 :(得分:0)

您可以这样使用UIPageviewController :(继承) https://medium.com/how-to-swift/how-to-create-a-uipageviewcontroller-a948047fb6af

答案 1 :(得分:0)

您可以随时致电setViewControllers(我一直在这样做)。

它不会产生预期结果的事实可能是您的代码中存在错误的结果,如果您想帮助它,请包含您的代码。

答案 2 :(得分:0)

感谢您的帮助,我弄清楚我做错了什么。我没有为ContainerView中的活动PageViewController提供正确的引用。

当然,在随机的PageViewController上调用setViewControllers函数并没有产生正确的结果。

I found this answer explains how to correctly get a reference from a ContainerView