gpt4 book ai didi

ios - 如何在 Swift 中正确推进 PageViewController

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

我正在尝试用 Swift 开发一个教程。我有一个使用指示器 ( http://www.appcoda.com/uipageviewcontroller-storyboard-tutorial/ ) 的页面 View Controller ,但我想添加一个按钮以通过单击它而不是滑动屏幕 (TODO NextPage) 转到下一页。

我试过:

let startVC = self.viewControllerAtIndex(self.index + 1) as TutorialContentViewController
let viewControllers = NSArray(object: startVC)
self.pageViewController.setViewControllers(viewControllers as! [UIViewController],
direction: UIPageViewControllerNavigationDirection.Forward,
animated: true,
completion: nil)

问题是圈子页面指示器没有更新,点击下一步后,所有页面都是第二页。

源代码

import UIKit

class TutorialViewController: UIViewController, UIPageViewControllerDataSource {

var pageViewController: UIPageViewController!
var pageTexts: NSArray!
var pageImages: NSArray!
var index: Int = 0

@IBOutlet var button: UIButton!

override func viewDidLoad() {
super.viewDidLoad()

self.pageTexts = NSArray(objects: "Explore", "Today Widget")
self.pageImages = NSArray(objects: "background1.jpg", "background2.jpg")

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

self.pageViewController.dataSource = self

let startVC = self.viewControllerAtIndex(index) as TutorialContentViewController
let viewControllers = NSArray(object: startVC)

self.pageViewController.setViewControllers(viewControllers as! [UIViewController], direction: .Forward, animated: true, completion: nil)
self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.size.height - 60)

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

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

func viewControllerAtIndex(index: Int) -> TutorialContentViewController {
if (index >= self.pageTexts.count) {
return TutorialContentViewController()
}

let vc: TutorialContentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("TutorialContentViewController") as! TutorialContentViewController

vc.image = self.pageImages[index] as! String
vc.text = self.pageTexts[index] as! String
vc.pageIndex = index
self.index = index

self.button.setTitle(self.index == self.pageTexts.count - 1 ? "Finish" : "Next", forState: .Normal)

return vc
}


// MARK: - Page View Controller Data Source
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
let vc = viewController as! TutorialContentViewController
let index = vc.pageIndex as Int

return self.viewControllerAtIndex(index)
}

func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
let vc = viewController as! TutorialContentViewController
var index = vc.pageIndex as Int

if (index == NSNotFound) {
return nil
}

index++
if (index == self.pageTexts.count) {
self.button.setTitle("Finish", forState: .Normal)
return nil
} else {
self.button.setTitle("Next", forState: .Normal)
}
return self.viewControllerAtIndex(index)
}

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

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

@IBAction func onButtonClick(sender: AnyObject) {
if (self.index == self.pageTexts.count - 1) {
let vc : UIViewController = self.storyboard!.instantiateViewControllerWithIdentifier("splashViewController")
vc.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve
self.presentViewController(vc, animated: true, completion:nil)
} else {
//TODO NextPage
}
}

最佳答案

这里 let viewControllers = NSArray(object: startVC) 你创建了一个 UIViewController 数组,其中只有一个 UIViewController (你的第二个 UIViewController)。

改为使用代码中的 viewControllerAtIndex 函数。

现在您的 UIButton 操作应该如下所示:

func nextPage(sender: AnyObject) {
self.pageViewController.setViewControllers([self.viewControllerAtIndex(self.index + 1)!], direction: .Forward, animated: true, completion: nil)
}

关于ios - 如何在 Swift 中正确推进 PageViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33443549/

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