LoginSignup
1
3

More than 5 years have passed since last update.

Swift2でMMDrawerContrllerを使って画面遷移する

Posted at

Swift2でMMDrawerControllerを使うサンプルは探せばいろいろ出てくるんですが、
サイドメニューから画面遷移を実装するサンプルがなく、探すのに少し手間取ったので、
記事にしておきます。

実際にコードを見たい方はこちら
サンプルコード

画面の構成
スクリーンショット 2016-06-26 22.12.07.png
MainViewControllerから画面1・画面2へのSegueのId
画面1 = toView1
画面2 = toView2
となっています。

コードの要点は
AppDelegate.swift
LeftDrawerViewController.swiftの2点です。
AppDelegateはMMDrawerの実装が記述されています。

LeftDrawerViewControllerはMainViewのSegueを使うためのコードが記述されています。
以下はLeftDrawerViewControllerのコードの抜粋です。

LeftDrawerViewController.swift
@IBAction func toView1(sender: UIButton) {
        toView("toView1")
}

@IBAction func toView2(sender: UIButton) {
        toView("toView2")
}

func toView(segueId: String) {
        let mainView = (self.mm_drawerController.centerViewController as! UINavigationController).childViewControllers[0] as! MainViewController
        mainView.performSegueWithIdentifier(segueId,sender: nil)
        let appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        appDelegate.centerContainer!.toggleDrawerSide(MMDrawerSide.Left,animated: true, completion:nil)
}

まず
let mainView = (self.mm_drawerController.centerViewController as! UINavigationController).childViewControllers[0] as! MainViewController
ここで、MainViewControllerを取得しています。

次に
mainView.performSegueWithIdentifier(segueId,sender: nil)
MainViewのperformSegueWithIdentifierを呼び出して、MainViewのSegueを利用します。

1
3
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
1
3