我正在尝试在Swift中开发一个教程。我有一个页面视图控制器使用指标(http://www.appcoda.com/uipageviewcontroller-storyboard-tutorial/),但我想添加一个按钮,通过单击而不是滑动屏幕(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
}
}
}
答案 0 :(得分:1)
在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)
}
操作应如下所示:
changeAccount