从集合中删除最后一个View中的PopViewController

时间:2015-04-02 09:13:13

标签: ios uipageviewcontroller

我开发了一款应用程序,其第一个视图带有“开始”按钮。当我按下按钮时,我开始PageViewController。我需要的是,在收集的最后一项中,在5秒后,使用“开始”按钮返回第一个视图。我试过self.dismissViewControllerAnimated,但它没有用。

当我点击“开始”按钮时,我调用带有标识符的笔尖:

 self.performSegueWithIdentifier("goToViewController", sender: nil)

此笔尖有一个自定义类别:

enter image description here

在这个课程中,我开发了PageViewController的行为,我将PageViewController内容配置为StoryboardID'

import UIKit
import AVFoundation

class PageViewController: UIViewController, UIPageViewControllerDataSource {

    var audioPagePlayer = AVAudioPlayer()
    var error: NSError?
    var audioPage = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Turn page", ofType: "mp3")!)
    var contentPageRestorationIDs = [String]()
    var pageViewController = UIPageViewController()

    override func viewDidLoad() {
        super.viewDidLoad()

        audioPagePlayer = AVAudioPlayer(contentsOfURL: audioPage, error: &error)

        contentPageRestorationIDs = ["FirstView", "SecondView", "ThirdView", "FourthView", "FiveView", "SixView", "SevenView", "EightView", "NineView", "TenView", "ElevenView", "TwelveView", "ThirteenView", "FourteenView"]
        // Do any additional setup after loading the view, typically from a nib.

        pageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PageViewController") as UIPageViewController
        pageViewController.dataSource = self

        var startingController: UIViewController = self.viewControllerAtIndex(0)!

        pageViewController.setViewControllers([startingController], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)

        self.addChildViewController(self.pageViewController)
        self.view.addSubview(self.pageViewController.view)
        self.pageViewController.didMoveToParentViewController(self)


    }

    func goToPreviousContentViewController(){

        var currentViewController: AnyObject = self.pageViewController.viewControllers[0]
        var vcRestorationID = currentViewController.restorationIdentifier
        var index = find(contentPageRestorationIDs, vcRestorationID!!)

        var previousViewController: UIViewController = self.viewControllerAtIndex(index! - 1)!

        pageViewController.setViewControllers([previousViewController], direction: UIPageViewControllerNavigationDirection.Reverse, animated: false, completion: nil)



    }

    func goToNextContentViewController(){

        var currentViewController: AnyObject = self.pageViewController.viewControllers[0]
        var vcRestorationID = currentViewController.restorationIdentifier
        var index = find(contentPageRestorationIDs, vcRestorationID!!)

        var nextViewController: UIViewController = self.viewControllerAtIndex(index! - 1)!

        pageViewController.setViewControllers([nextViewController], direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)



    }

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

    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> (UIViewController?){
        var vcRestorationID = viewController.restorationIdentifier
        var index = find(contentPageRestorationIDs, vcRestorationID!)

        if(index == 0){
            return nil
        }

        audioPagePlayer.prepareToPlay()
        audioPagePlayer.play()


        return viewControllerAtIndex(index! - 1)
    }

    func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> (UIViewController?){
        var vcRestorationID = viewController.restorationIdentifier
        var index = find(contentPageRestorationIDs, vcRestorationID!)

        if(index == contentPageRestorationIDs.count - 1){
            return nil
        }

        var vc = viewController as BaseContentViewController

        if(vc.goNext == true){

            audioPagePlayer.prepareToPlay()
            audioPagePlayer.play()

            return viewControllerAtIndex(index! + 1)

        }
        return nil
    }


    func presentationCountForPageViewController(pageViewController: UIPageViewController) -> (Int){
        return self.contentPageRestorationIDs.count
    }

    func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> (Int){
        return 0
    }


    func viewControllerAtIndex(index: Int)-> (BaseContentViewController!) {

        if(index >= contentPageRestorationIDs.count){
            return nil
        }

        var storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle(forClass: self.dynamicType))

        var contentViewController: BaseContentViewController = storyboard.instantiateViewControllerWithIdentifier(self.contentPageRestorationIDs[index]) as BaseContentViewController

        contentViewController.rootViewController = self

        contentViewController.loadView()

        return contentViewController

    }
}

每个笔尖的视图控制器都来自自定义类BaseContentViewController

import UIKit

class BaseContentViewController: UIViewController {

    var rootViewController: PageViewController = PageViewController()
    var goNext = Bool()


    override func viewDidLoad() {
        super.viewDidLoad()

        println("View Did Load Form Base Content View Controller")
        // Do any additional setup after loading the view.
    }

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



}

当我在视图中使用de StoryboardID" FourteenView",5秒后,我需要在PageViewController之前返回视图

3 个答案:

答案 0 :(得分:1)

你是如何展示PageViewController的?如果你正在推动它,那么你必须弹出它,如果你把它作为模型呈现,那么使用dismissViewControllerAnimated !!

答案 1 :(得分:0)

您可以像这样使用performSelector延迟

[self performSelector:@selector(functionToDismiss) withObject:nil afterDelay:5.0];

其中functionToDismiss应包含解除当前视图控制器的代码

答案 2 :(得分:0)

好的,

解决方案放在一个函数内,此代码在延迟后调用:

func finish(){

    self.rootViewController.navigationController?.popViewControllerAnimated(true)

}