LoginSignup
4
9

More than 5 years have passed since last update.

[Swift3] UIPageViewControllerの使い方

Last updated at Posted at 2017-02-26

この記事は、【決定版】UIPageViewControllerの使い方(Swift)のコードをSwift3で書き直したものです。
StoryBoardの設定などは上記の記事で確認してください。

swift3
import UIKit

class PageViewController: UIPageViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.setViewControllers([getFirst()], direction: .forward, animated: true, completion: nil)
        self.dataSource = self

    }

    func getFirst() -> FirstViewController {
        return storyboard!.instantiateViewController(withIdentifier: "FirstViewController") as!FirstViewController
    }
    func getSecond() -> SecondViewController {
        return storyboard!.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
    }
    func getThird() -> ThirdViewController{
        return storyboard!.instantiateViewController(withIdentifier: "ThirdViewController") as!ThirdViewController
    }
}

    extension PageViewController : UIPageViewControllerDataSource {

        func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
            if viewController.isKind(of: ThirdViewController.self) {
                // 3 -> 2
                return getSecond()
            } else if viewController.isKind(of: SecondViewController.self) {
                // 2 -> 1
                return getFirst()
            }else{
                // 1 -> end of the road
                return nil
            }
        }

        func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
            if viewController.isKind(of: FirstViewController.self) {
                // 1 -> 2
                return getSecond()
            } else if viewController.isKind(of: SecondViewController.self) {
                // 2 -> 3
                return getThird()
            }else{
                // 3 -> end of the road
                return nil
            }
        }
    }

4
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
9