Skip to content

Instantly share code, notes, and snippets.

@KentarouKanno
Last active June 10, 2016 23:58
Show Gist options
  • Save KentarouKanno/1e477016719e0385e5a6 to your computer and use it in GitHub Desktop.
Save KentarouKanno/1e477016719e0385e5a6 to your computer and use it in GitHub Desktop.
Segue Template

Segue Template

★ 画面遷移

@IBAction func pushButton(sender: UIButton) {
    // 画面遷移呼び出し
    performSegueWithIdentifier("segueIdentifier", sender: self)
}

// 画面遷移前処理
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    
    if segue.identifier == "segueIdentifier" {
        // 遷移先のViewControllerを取得
        let nextVC = segue.destinationViewController as! NextViewController
        // 値を渡す
        nextVC.item = self.item
    }
}

★ StoryboardのNavigationで2画面先に遷移する

// 1つ目のViewControllerへのPush Animationは無く、2つ目のみAnimation有り

guard var history = self.navigationController?.viewControllers,
    let detilView = self.storyboard?.instantiateViewControllerWithIdentifier("detail") as? DetailViewController,
    let editView  = self.storyboard?.instantiateViewControllerWithIdentifier("edit")   as? EditiewController else {
    return
}

history.append(detilView)
history.append(editView)
self.navigationController?.setViewControllers(history, animated: true)

★ Modalを出した親のVCをPushで2つすすめる(Modalを閉じた時に2つ先のVCに着地する)

// ListViewController
class ListViewController: UIViewController {

    @IBAction func pushModal(sender: UIButton) {
        if let modalView = self.storyboard?.instantiateViewControllerWithIdentifier("modal") as? ModalViewController {

            guard var history = self.navigationController?.viewControllers,
                let detilView = self.storyboard?.instantiateViewControllerWithIdentifier("detail") as? DetailViewController,
                let editView = self.storyboard?.instantiateViewControllerWithIdentifier("edit")   as? EditiewController else {
                    return
            }

            history.append(detilView)
            history.append(editView)
            modalView.editView = editView

            self.presentViewController(modalView, animated: true) {
                self.navigationController?.setViewControllers(history, animated: false)
            }
        }
    }
}

// ModalViewController
class ModalViewController: UIViewController {

    weak var editView: EditiewController!

    @IBAction func pushCloseButton(sender: UIButton) {
        // 親のViewControllerからModalを閉じる
        editView?.modalClose(self)
    }
}

// DetailViewController
class DetailViewController: UIViewController {

    @IBAction func pushNext(sender: UIButton) {
        if let editView = self.storyboard?.instantiateViewControllerWithIdentifier("edit") as? EditiewController {
            self.navigationController?.pushViewController(editView, animated: true)
        }
    }
}

// EditViewController
class EditiewController: UIViewController {

    var modalView: ModalViewController!

    func modalClose(modalView: ModalViewController) {
        modalView.dismissViewControllerAnimated(true, completion: nil)
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment