0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[ios]UIPageViewControllerの使い方

Posted at

UIPageViewControllerを使ったので、メモ。
ようやく、storyboardでVCを扱わず、コードのみで処理を行う感覚が掴めてきた気がする。
余談だが、12月からアウトプットする習慣をつけたいと考えて、毎日投稿するようにしているのだが、なかなか難しい。けど、アウトプットすることを意識してコードを書くだけで、いつもの2、3倍知識を吸収できている気がする。

SlideViewController.swift
import UIKit

class SlideViewController: UIPageViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.setViewControllers([getChat()], direction: .forward, animated: true, completion: nil)
        self.dataSource = self as! UIPageViewControllerDataSource
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func getChat() -> ChatViewController {
        return storyboard!.instantiateViewController(withIdentifier: "chat") as! ChatViewController
    }
    
    func getDetail() -> GroupDetailViewController {
        return storyboard!.instantiateViewController(withIdentifier: "detail") as! GroupDetailViewController
    }
    
}

extension SlideViewController: UIPageViewControllerDataSource {
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        if viewController.isKind(of:GroupDetailViewController.self) {
            return getChat()
        }  else {
            return nil
        }
        
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        if viewController.isKind(of:ChatViewController.self) {
            return getDetail()
        }  else {
            return nil
        }
        
    }
    
}
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?