UIPageViewController下一步按钮

时间:2015-10-29 07:12:49

标签: ios swift

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

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

}

2 个答案:

答案 0 :(得分:2)

您可以使用:

下一页

@IBAction func onButtonNextClick(sender: object) {
    pageViewController.setViewControllers(viewControllers,
                                                    direction: UIPageViewControllerNavigationDirection.Forward,
                                                    animated: true,
                                                    completion: nil)
}

上一页

@IBAction func onButtonPreviousClick(sender: object) {
    pageViewController.setViewControllers(viewControllers,
                                                    direction: UIPageViewControllerNavigationDirection.Reverse,
                                                    animated: true,
                                                    completion: nil)
}

答案 1 :(得分:0)

一种快速的方法是将UIPageViewController子类化,并使用数据源和委托将按钮操作放在那里,如下所示:

@implementation MyPageViewController

- (IBAction)nextPage:(id)sender{
    NSArray *previousControllers = self.viewControllers;
    UIViewController *controller = [self.dataSource pageViewController:self viewControllerAfterViewController:previousControllers.firstObject];
    if(!controller){
        return;
    }
    __weak typeof(self) weakSelf = self;
    [self setViewControllers:@[controller] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:^(BOOL finished) {
        [weakSelf.delegate pageViewController:weakSelf didFinishAnimating:finished previousViewControllers:previousControllers transitionCompleted:YES];
        // if you need more than one line of code you must make a strongSelf = weakSelf variable first
    }];
}

如果在页面边界,您还可以使用presentationCountForPageViewControllerpresentationIndexForPageViewController来禁用/启用按钮。