如何跳到UIPageViewController中的特定页面,setViewControllers不起作用

时间:2018-08-05 17:22:38

标签: ios swift xcode viewcontroller uipageviewcontroller

我试图在按下按钮时跳到特定页面,我制作了一个ViewController,其中包含PageViewController的ContainerView,以便可以在顶部放置永久按钮。我从那里开始:

@IBAction func jump(_ sender: Any) {
    PageViewController().jump()
}

这是PageViewController中的Jump函数

func jump() {

    var lastCoinNumber:Int = Manager.shared.coins.index(of: last!)!
    lastNumber = lastCoinNumber

    if let jumpView = viewControllerAtIndex(lastNumber, storyboard: self.storyboard!) {
        self.setViewControllers([jumpView], direction: .forward, animated: false, completion: nil)
    }
}

由于某种原因,我总是在跳转函数的ViewControllerAtIndex部分得到一个错误:

  

线程1:致命错误:展开一个可选值时意外发现nil   。即使我手动选择一个索引也可以做到。

if let firstViewController = viewControllerAtIndex(0, storyboard: self.storyboard!) {    
    self.setViewControllers([firstViewController], direction: .forward, animated: false, completion: nil)
}

以上是我如何在viewDidLoad()中设置ViewControllers的初始方法,该方法很好,如果我本质上在做同样的事情,为什么它不起作用?这是我为页面建立索引并实例化视图控制器的方式,它们使用相同的单个ViewController,因此将为每个页面创建一个新实例,否则我将创建一个viewControllers数组。

func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) { if !completed { return }
    DispatchQueue.main.async() {
        self.dataSource = nil
        self.dataSource = self
        self.pageControl.numberOfPages = (Manager.shared.coins.count)
    }
}

func viewControllerAtIndex(_ index: Int, storyboard: UIStoryboard) -> TemplateViewController? {

    if Manager.shared.coins.count == 0 || index >= Manager.shared.coins.count {
        return nil
    }

    let templateViewController = storyboard.instantiateViewController(withIdentifier: "templateController") as! TemplateViewController

    templateViewController.dataObject = Manager.shared.coins[index]
    return templateViewController
}

func indexOfViewController(_ viewController: TemplateViewController) -> Int {
    self.pageControl.currentPage = Manager.shared.coins.index(of: viewController.dataObject)!
    return Manager.shared.coins.index(of: viewController.dataObject)!
}

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

    var index = self.indexOfViewController(viewController as! TemplateViewController)
    if (index == 0) {
        return nil
    }
    index -= 1
    return self.viewControllerAtIndex(index, storyboard: viewController.storyboard!)
}

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

    var index = self.indexOfViewController(viewController as! TemplateViewController)
    if index == -1 {
        return nil
    }
    index += 1
    if index == Manager.shared.coins.count {
        return nil
    }
    return self.viewControllerAtIndex(index, storyboard: viewController.storyboard!)
}

我希望这是足够的信息,谢谢。

编辑:

硬币是一个字符串数组,我这样声明:

var last = Manager.shared.coins.last
var lastNumber: Int = 0

,然后jump()负责其余的工作。因此,我得到了最后一个对象,找到了它的索引,然后用最后一个数字的索引进行了setViewControllers,该数字应该与viewControllerAtIndex相对应,因为它使用相同的数组来索引和加载页面。

由于某种原因,当我尝试执行以下操作时:     var lastCoinNumber:Int = Manager.shared.coins.index(of:last) 它迫使我使用!因为:     “可选类型'Array.Index的值? (又称“可选”)未包装;您是说要使用“!”或“?”?”

我不确定在这方面做错了什么,这是硬币的声明方式:

var coins = [""]

编辑:完整代码,PageViewController:

import UIKit

var pageView = PageViewController()

class PageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {

    //page Control dots
    var pageControl = UIPageControl()
    var last = Manager.shared.coins.last!
    var lastNumber: Int = 0


    var viewControllerArray = [UIViewController()]


    func configurePageControl() {
    // The total number of pages that are available is based on ManagerCoins names array
        pageControl = UIPageControl(frame: CGRect(x: 0,y: 
    UIScreen.main.bounds.maxY - 50,width: UIScreen.main.bounds.width,height: 
    50))
        self.pageControl.numberOfPages = (Manager.shared.coins.count)
        self.pageControl.currentPage = 0
        self.pageControl.tintColor = UIColor.black
        self.pageControl.pageIndicatorTintColor = UIColor.gray
        self.pageControl.currentPageIndicatorTintColor = UIColor.white
        self.view.addSubview(pageControl)
    }

    // MARK: Delegate functions
    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {

    }

    func viewControllerAtIndex(_ index: Int, storyboard: UIStoryboard) -> TemplateViewController? {

        if Manager.shared.coins.count == 0 || index >= Manager.shared.coins.count {
            return nil
        }

        let templateViewController = storyboard.instantiateViewController(withIdentifier: "templateController") as! TemplateViewController

        templateViewController.dataObject = Manager.shared.coins[index]
        return templateViewController
    }

    func indexOfViewController(_ viewController: TemplateViewController) -> Int {
        self.pageControl.currentPage = Manager.shared.coins.index(of: viewController.dataObject)!
        return Manager.shared.coins.index(of: viewController.dataObject)!
    }

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

        var index = self.indexOfViewController(viewController as! TemplateViewController)
        if (index == 0) {
            return nil
        }
        index -= 1
        return self.viewControllerAtIndex(index, storyboard: viewController.storyboard!)
    }

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

        var index = self.indexOfViewController(viewController as! TemplateViewController)
        if index == -1 {
            return nil
        }
        index += 1
        if index == Manager.shared.coins.count {
            return nil
        }
        return self.viewControllerAtIndex(index, storyboard: viewController.storyboard!)
    }

    func jump() {

        var lastCoinNumber:Int = Manager.shared.coins.index(of: last!)!
        lastNumber = lastCoinNumber

        if let jumpView = viewControllerAtIndex(lastCoinNumber, storyboard: self.storyboard!){
            self.setViewControllers([jumpView], direction: .forward, animated: false, completion: nil)
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.delegate = self
        configurePageControl()

        self.dataSource = self

        if let firstViewController = viewControllerAtIndex(0, storyboard: self.storyboard!){

            self.setViewControllers([firstViewController], direction: .forward, animated: false, completion: nil)
        }
    }
}

编辑:

经过大量研究,这似乎是通过调用setViewControllers来跳转页面的正确方法,对此答案的第一个注释似乎也有相同的问题,它可以在viewDidLoad()上设置视图,但效果很好基于索引落入错误以编程方式导航到页面。 https://stackoverflow.com/a/24335008/10058239

1 个答案:

答案 0 :(得分:0)

我认为它是由于强制展开而崩溃的:

var lastCoinNumber:Int = Manager.shared.coins.index(of: last!)!

如果硬币是收藏夹,则可以使用 .last 来获取最后一个元素,例如:

let numbers = [10, 20, 30, 40, 50]
if let lastNumber = numbers.last {
    print(lastNumber)
}
// Prints "50"