问题
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