0
1

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.

[Swift5] 画面遷移の実装

Last updated at Posted at 2021-03-13

1, シンプルな画面遷移

Storyboard で Segueに Identifier を設定
image.png

// 画面遷移
performSegue(withIdentifier: "next", sender: nil)

// 画面を消去して戻る場合
dismiss(animated: true, completion: nil)

画面遷移時に値を渡す

遷移先のViewControllerを segue.destination でインスタンス化してプロパティを更新する
Example: 次の画面のcount2に値を渡したい場合

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let nextVC = segue.destination as! NextViewController
        nextVC.count2 = count
}

2,Navigation Controllerで画面遷移する

  • Product -> Embed -> Navigation Controller でNavigation Controllerを追加する
    image.png

  • storyboard?.instantiateViewController でインスタンスをつくってnavigationController?.pushViewController で遷移させる

let nextVC = storyboard?.instantiateViewController(identifier: "next") as! NextViewController
navigationController?.pushViewController(nextVC, animated: true)
  • storyboard? を使う時は、storyboard id を設定すること
    image.png
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?