1
3

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.

画面遷移の実装方法まとめ[Xcode/Storyboard]

Posted at

Xcode/Storyboardで、画面遷移の実装方法をアウトプットしてみようと思います。

#環境
・Mac Book Pro (macOS:Big Sur)
・Xcode(ver:12.5)

#画面遷移①(Storyboardのみ)
1.storyboardでController内のButtonからControllerにSegueをつなぐ。
2.ShowかPresent Modallyを選択
Unknown.gif

#画面遷移②(performSegueで遷移)
1.ViewController同士をSegueでつなぐ
2.ShowかPresent Modallyを選択
3.Segueのidentifierを付ける(下記のコードの場合は「next」)
4.画面遷移したいタイミングで下記のコードを書く
Unknown-1.gif

ViewController.swift
class ViewController: UIViewController {

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

    
    @IBAction func next(_ sender: Any) {
        
        performSegue(withIdentifier: "next", sender: nil)
    }
    
}

#画面遷移③(performSegue+prepareで値を渡して遷移)
1.ViewController同士をSegueでつなぐ
2.ShowかPresent Modallyを選択
3.Segueのidentifierを付ける(下記のコードの場合は「next」)
4.画面遷移したいタイミングで「performSegue」を書く
5.prepareで値を渡したい処理内容を書く

ViewController.swift
class ViewController: UIViewController {
    
    var str = "画面遷移成功"
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    
    @IBAction func next(_ sender: Any) {
        
        performSegue(withIdentifier: "next", sender: nil)
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        
        let nextVC = segue.destination as! NextViewController
        
        nextVC.str = str
    }
    
}
NextViewController.swift
class NextViewController: UIViewController {
    
    "値を受け取る変数を用意"
    var str = String()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

#画面遷移④(NavigationController+ Storyboard IDで画面遷移)
1.ViewControllerを選択した状態で、Editor > Embed in > Navigation Controllerを選択
2.画面遷移するViewControllerのStoryboardIDを記述
3.Use Storyboard IDにチェックを入れる
4.下記のようにコードを書く

ViewController.swift
class ViewController: UIViewController {
    
    var str = "画面遷移成功"
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    
    @IBAction func next(_ sender: Any) {
        
        let nextVC = self.storyboard?.instantiateViewController(identifier: "nextVC") as! NextViewController
        
        nextVC.str = str
        
        self.navigationController?.pushViewController(nextVC, animated: true)
    }
}
NextViewController.swift
   class NextViewController: UIViewController {

    var str = String()
    
    override func viewDidLoad() {
        super.viewDidLoad()

      print(str)
    }
}

以上で、自分なりに画面遷移の方法をまとめてみました。

Udemyでメモしたコードを自分なりに理解してアウトプットしていこうと思います。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?