[Swift] Implement screen transition and pass by value with Segue and present

What to make this time

Perform screen transitions with Segue and present. If you press the "Jump with Segue" button on the first screen (View Controller), you will move to the second screen (Second View Controller). If you press the "Jump with present" button on the second page, you will be taken to the third screen (ThirdViewController). The colors are the same as the button color and the background color of the transition destination. Simulator Screen Shot - iPhone 12 mini - 2020-12-14 at 18.39.50.pngSimulatorScreenShot-iPhone12mini-2020-12-14at18.37.03.pngSimulatorScreenShot-iPhone12mini-2020-12-14at18.37.42.png . . ViewController . . SecondViewController . . ThirdViewController . .

I'm using the code I made earlier in [Swift] The last saucer for beginners who don't know Delegate. (See there for a detailed delegate description)

Screen transition using Segue

スクリーンショット 0002-12-14 20.25.00.pngスクリーンショット0002-12-1420.25.40.png

  1. Match the Identifer with the segue withIdentifier.
  2. Here, we want to display "Transitioned by segue" in the transition destination (SecondViewController), so use prepare to enter the information first. ** Prepare runs before transitioning with segue, so pass by value with prepare **

ViewController.swift


    func onTapYellowButton() {
        //Make a screen transition to SecondViewController
        self.performSegue(withIdentifier: "toSecondViewController", sender: nil) //Use the same Identifier as storyboad
    }
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        switch segue.identifier! {
        case "toSecondViewController": //Segue Identifier
            let secondVC: SecondViewController = segue.destination as! SecondViewController
            secondVC.word = "Transitioned with segue" //Pass a value to SecondViewController
            break
        default:
            break
        }
    }

SecondViewController.swift


    var word: String = "";
    @IBOutlet weak var label: UILabel?
    @IBOutlet weak var button: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .yellow //Make the background color yellow
        label?.text = word //to word by prepare"Transitioned with segue"Is included
    }

Screen transition using present

With Segue, you have to connect when changing screens, but with present, you don't have to. Therefore, the screen can be transitioned to another NavigationController. スクリーンショット 0002-12-14 20.46.29.png By the way, what is NavigationController? As a supplement for those who say, NavigationController is like a frame. It means that the TableView on it is placed. The image looks like this. スクリーンショット 0002-12-14 20.53.04.png So when I use NavigationController, it looks like it has a frame on top.

Aside from that, I'll go into the implementation.

  1. Match the NavigationController's Identifier with the withIdentifier in your code. スクリーンショット 0002-12-14 21.05.18.png
  2. Pass the value to ThirdViewController before present

SecondViewController.swift


@IBAction func buttonTap(_ sender: Any) {
        let navi: UINavigationController = self.storyboard?.instantiateViewController(withIdentifier: "NaviForController") as! UINavigationController
        let nextVC: ThirdViewController = navi.viewControllers[0] as! ThirdViewController
        nextVC.word = "Screen transition with present" //Pass a value to the word of ThirdViewController
        self.present(navi, animated: true, completion: nil) //Screen transition to ThirdViewController
    }

ThirdViewController.swift


    var word: String = ""
    @IBOutlet weak var label: UILabel?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .green //Make the background color green
        label?.text = word //SecondViewController nextVC.word = "Screen transition with present"Is included by
    }

Finally

How was that? Personally, it was a shock when I first learned that I always go through prepare when using it during Segue. I haven't specified it this time, but it's easy to forget to match the storyboard class with the code class, so you have to remember it. Storyboard and all the code for your reference スクリーンショット 0002-12-14 21.20.07.png スクリーンショット 0002-12-14 21.17.22.png

Recommended Posts

[Swift] Implement screen transition and pass by value with Segue and present
Java pass by value and pass by reference
[Swift] Simple screen transition summary
Screen transition with swing, java
[Swift5] How to communicate from ViewController to Model and pass a value
Screen transition by Post method [Java]
[Java Swing] Screen transition by CardLayout