Swift - 浏览UIPageViewController中3个不同视图控制器之间的按钮

时间:2016-10-03 16:30:18

标签: ios swift uipageviewcontroller

所以我一直在尝试使用UIPageViewController,我有三种不同的视图控制器(3种不同的类)

所以我已经成功了!我可以完美地从View Controller滑动到View Controller!在这三个类的viewDidLoad中,我分别创建了self.view.tag = 0,1,2,所以我知道我当前的View Controller是哪个。这是我的RootViewController的代码:

import UIKit

class ViewController: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
@IBOutlet var bottomView: UIView!

var pageViewController: UIPageViewController!
var currentVCId = 0

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    pageViewController = self.storyboard?.instantiateViewController(withIdentifier: "PageViewController") as! UIPageViewController
    pageViewController.dataSource = self
    pageViewController.delegate = self

    let starterVC = viewControllerWith(id: 0)

    pageViewController.setViewControllers([starterVC], direction: .forward, animated: false, completion: nil)
    pageViewController.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)

    self.addChildViewController(pageViewController)
    self.view.addSubview(pageViewController.view)
    self.view.bringSubview(toFront: bottomView)

    pageViewController.didMove(toParentViewController: self)
}

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

func viewControllerWith(id: Int) -> UIViewController {
    if id == 0 {
        return self.storyboard?.instantiateViewController(withIdentifier: "FirstPageContentViewController") as! FirstPageContentViewController
    }
    else if id == 1 {
        return self.storyboard?.instantiateViewController(withIdentifier: "SecondPageContentViewController") as! SecondPageContentViewController
    }
    else {
        return self.storyboard?.instantiateViewController(withIdentifier: "RandomColorViewController") as! RandomColorViewController
    }
}

func getViewControllerIdFor(previousViewController: Bool) -> Int? {
    if previousViewController == true {
        return currentVCId == 0 ? nil : currentVCId - 1
    }

    return currentVCId == 2 ? nil : currentVCId + 1
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
    if let returnIdValue = getViewControllerIdFor(previousViewController: true) {
        return viewControllerWith(id: returnIdValue)
    }

    return nil
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
    if let returnIdValue = getViewControllerIdFor(previousViewController: false) {
        return viewControllerWith(id: returnIdValue)
    }

    return nil
}

func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
    guard completed else {
        return
    }

    let viewController = pageViewController.viewControllers![0]
    currentVCId = viewController.view.tag
}

@IBAction func goToA(_ sender: UIButton) {
    if currentVCId != 0 {
        let currentVC = pageViewController.viewControllers?[0]
        let previusVC = pageViewController(pageViewController, viewControllerBefore: currentVC!)

        pageViewController.setViewControllers([previusVC!], direction: .reverse, animated: true, completion: nil)
        currentVCId = 0
    }
}

}

问题是,我已经为我的RootViewController添加了2个按钮,无需刷卡即可从View Controller切换到View Controller。所以,让我们假设我已经刷到第二个View Controller并且我想回到我的第一个,按下左键。记住我的第一个VC已经加载了,所以我可以修改它的内容..

所以现在,如果我按下按钮,它的代码是上面的最后一个方法,它将导航回来,但它将创建所需VC的新实例!我想做的是显示我已经加载的VC,好像我刷了......就像Snapchat ......

关于如何实现这一点的任何想法?

1 个答案:

答案 0 :(得分:1)

这来自instantiateViewController(withIdentifier:)

上的documentation
  

每次调用时,此方法都会创建指定视图控制器的新实例。

这就是你获得新实例的原因。我会推荐一种不同的方法。而不是让pageViewController跟踪VC并通过索引和标记盲目地访问它们,为什么不将它们作为属性存储在根目录中呢?这将允许您确保VC的相同实例,并且可以让您远离检查标记(这是脏的)到更可读的内容,例如检查与实际VC对象的相等性。