gpt4 book ai didi

ios - 从集合中关闭最后一个 View 中的 PopViewController

转载 作者:行者123 更新时间:2023-11-29 12:25:02 25 4
gpt4 key购买 nike

我开发了一个应用程序,它的第一个 View 带有“开始”按钮。当我按下按钮时,我启动了 PageViewController。我需要的是,在集合的最后一项中,5 秒后,使用“开始”按钮返回到第一个 View 。我尝试使用 self.dismissViewControllerAnimated 但它没有用。

当我点击“开始”按钮时,我调用了一个带有标识符的 Nib :

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

这个 nib 分配了一个自定义类:

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

}
}

每个 nib 的 View Controller 都从自定义类 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.
}



}

当我在 StoryboardID 为“FourteenView”的 View 中时,5 秒后,我需要返回到 PageViewController 之前的 View

最佳答案

您是如何显示 PageViewController 的?如果你正在插入它,那么你必须弹出它,如果你将它作为模型呈现,那么使用 dismissViewControllerAnimated!!

关于ios - 从集合中关闭最后一个 View 中的 PopViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29409187/

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