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 1 year has passed since last update.

【超初心者向け】画面遷移について

Last updated at Posted at 2023-03-04

最初に

この記事はswiftを勉強し始めた超初心者による超初心者のためのものになっているので、細かいミスなどが散見されるかもしれませんがご了承ください。

画面遷移の方法

①segueとボタンのみで遷移する

スクリーンショット 2022-08-02 午後8.35.28.png
多分一番最初に学ぶやり方です。storyboard 上に button を置き、そこから別の viewController につなげます。その時、遷移の種類が求められるので、行いたい遷移に従って設定しましょう。これで、その button を押すと画面遷移がすることができるようになります。

②segueとコードを利用した画面遷移

スクリーンショット 2022-08-02 午後10.47.53.png

ViewController.swift
class ViewController: UIViewController{

   override func viewDidLoad() {
      super.viewDidLoad()
   }

   @IBAction func next(_ sender: UIButton){
      self.performSegue(withIdentifier: "toNext", sender: nil)
   }
}

segueidentifier を設定し、それに応じて画面遷移をする方法です。

③コードのみを利用した画面遷移

ViewController.swift
class ViewController: UIViewContorller{

   override func viewDidLoad() {
      super.viewDidLoad()
   }

   @IBAction func next(_ sender: UIButton){
      //遷移先のViewControllerが存在するstoryboardを宣言する。(この場合はこのViewControllerと同じところに存在するため"self")
      let storyboard: UIStoryboard = self.storyboard!
      //遷移先のViewControllerを宣言する
      let nextViewController = storyboard.instantiateViewController(withIdentifier: "Next")
      //フルスクリーンで表示させる
      nextViewController?.modalPresentationStyle = .fullScreen
      //遷移の種類をcoverVerticalにする
      nextViewControlle?.modalTransitionStyle = .coverVertical
      //宣言したViewControllerを出現させる
      self.present(nextViewController, animated: true, completion: nil)
   }
}

storyboard 上で segue を設定せずに、コードのみで画面遷移を行う方法です。この場合、segue identifier をつけたのと同様、遷移先のViewControlleridentifierをつける必要があります。ちなみに、遷移の種類についてはアニメーション付きで分かりやすい記事が他にあるのでそちらを見てください。

④NavigationControllerを使用した画面遷移

スクリーンショット 2022-08-03 午前0.20.38.png
NavigationControllerを使用している場合、遷移の種類を"show"(push遷移)にすると、遷移の方向が横になり、自動的に戻るボタンも追加されます。

画面遷移の戻り方

①segueを使用している場合(NavigationController無し)

NextViewController.swift
class NextViewController: UIViewController{

   override func viewDidLoad() {
      super.viewDidLoad()
   }

   @IBAction func back(_ sender: UIButton){
      self.dismiss(animated: true, completion: nil)
   }
}

②NavigationControllerを使用している場合

NavigationControllerを使用している場合は、上述のように自動的に戻るボタンが設定されているので新たにbutton等を設定する必要はありませんが、コードを使用して戻る場合は下記のようになります。

NextViewContrller.swift
class NextViewController: UIViewController{

   override func viewDidLoad() {
      super.viewDidLoad()
   }

   @IBAction func back(_ sender: UIButton){
      self.navigationController?.popViewController(animated: ture)
   }
}

最後に

いろいろ細かいミスがあると思いますので、何かありましたらご指摘お願いします。

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?