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 3 years have passed since last update.

Modalで遷移した画面からUnwindSegueで戻る

Last updated at Posted at 2021-06-18

はじめに

  • 自分のメモとして記事を書いています。

参考文献

https://youtu.be/V3ippqWGH3o?list=PLQ5rERkGSxF9_soz3Ns-SpURWsy0WmbJQ

PresentModallyで接続する

  1. まず練習のための画面を用意します。今回は初めの画面と次の画面を
    用意し、間にナビゲーションコントローラを挟んでいます。
  2. 実際に画面をモーダルでつなぎます。初めの画面のボタンをドラックアンドドロップでナビゲーションコントローラに(PresentModallyで)つなぎます。

Unwind1.001.jpegUnwind1.002.jpeg

FullScreenにする

  1. segueを選択してFullScreenを選択します。
    Unwind1.003.jpeg

UnwindSegueのコードを書く

  1. 初めの画面のコントローラに移動してコードを書きます。
  2. 今回はViewController.swiftにコードを書きます。
    Unwind1.005.jpeg
@IBAction func exit(segue: UIStoryboardSegue) {
    }

遷移先の画面をUnwindSegueに接続

  1. ボタンをドラックアンドドロップでExitとつなぎます。
  2. コードで作成したUnwindSegueがあるのでそれを選択します。Unwind1.006.jpeg

Unwind1.007.jpeg

動画

unwindSegue.gif

 おまけ1

UnwindSegueのidentifierを設定して遷移した画面から戻る

UnwindSegueを接続する

  1. SecondViewControllerからドラックアンドドロップで、作成したUnwindSegueに接続する
    unwindSegue.001.jpeg
    unwindSegue.002.jpeg

identifierを設定する

  1. UnwindSegueを選択し、identifierを設定します。
  2. 今回はexitに設定しています。

名称未設定.001.jpeg

ボタンを接続する

  1. ボタンをドラックアンドドロップして、@IBAction接続する

unwindSegue.003.jpeg

ボタンが押された時の処理

  1. performSegueのwithIdentifierに設定したものをString型で入れます。
 @IBAction func tappedBtn(_ sender: Any) {
        performSegue(withIdentifier: "exit", sender: nil)
    }

unwindSegue.004.jpeg

おまけ2

画面を戻った時に遷移先のコントローラーを取り出す

1.今回はViewController内でSecondViewControllerを取り出しています。
※ UnwindSegueでは、SourceViewControllerが遷移先(今回ではSecondViewController)になる

 @IBAction func exit(segue: UIStoryboardSegue) {
        let secondViewController = segue.source as! SecondViewController
    }

名称未設定.001.jpeg

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?