How to properly advance a PageViewController in Swift

孤街浪徒 提交于 2019-12-12 06:24:44

问题


I am trying to develop a tutorial in Swift. I have a page view controller working with the indicators (http://www.appcoda.com/uipageviewcontroller-storyboard-tutorial/) but I want to add a button to go to the next page by clicking it instead of swiping the screen (TODO NextPage).

I've tried:

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)

The problem is that circle page indicator is not being updated and after clicking next, all the pages are the second page.

Source code

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
    }
}

}


回答1:


Here let viewControllers = NSArray(object: startVC) you make an array of UIViewController with only one UIViewController (your second UIViewController).

Instead use the viewControllerAtIndex function from your code.

So now your UIButton action should look like this:

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


来源:https://stackoverflow.com/questions/33443549/how-to-properly-advance-a-pageviewcontroller-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!