[Swift] How to use Unwind segue

Introduction

I used Unwind segue for screen transitions that are indispensable for creating apps, so I'll try to summarize it in my own way. When I first heard this name, it seemed pretty difficult, but the code was pretty simple.

Usage

I tried to implement

Try to make some things that come back from the previous screen

Prepare a view as shown in the picture below, and write the code so that it returns from the green screen to the blue screen. スクリーンショット 2020-12-26 16.08.42.png

Blue screen view controller code

To use Unwind segue, write the following code on the screen you want to return to. @IBAction func bluesegue(segue: UIStoryboardSegue) {} Try setting this function name yourself. Actually, this is the only code.

class ViewController: UIViewController {

    @IBAction func bluesegue(segue: UIStoryboardSegue) {
    }
}

Set Unwind segue on storyboard

Set to return to the blue screen from the third view controller button on the green screen. While holding down control from the button, drag and drop it to the exit marked with the red frame in the image. スクリーンショット 2020-12-26 16.29.22.png

The name of the function you set earlier will be displayed, so click it. This completes the linking. スクリーンショット 2020-12-26 16.24.50.png Unwind segue has been added as shown in red. スクリーンショット 2020-12-26 16.26.45.png

I tried to move

ダウンロード (1).gif

When transitioning after passing a value before returning

Add a textField to the one created above and pass a value to the label of the blue screen at the time of screen transition. スクリーンショット 2020-12-26 17.36.21.png

Green screen code

When the button is pressed, the text of the textField is assigned to the variable content, and the screen transitions with Unwind and the value is passed. This time, the screen transition will be after the process of entering the value, so it is not a connection from the button. Connect to Exit from the red frame ThirdViewController in the image below. スクリーンショット 2020-12-27 11.18.12.png Then set the Identifier to Unwind segue. By doing so, you can return with Unwind segue only when you specify the Identifier. スクリーンショット 2020-12-27 11.23.47.png

code

When the button is pressed, it is assigned and the screen transition of the Identifier set earlier with perform Segue is performed.

class ThirdViewController: UIViewController {
    @IBOutlet var textField: UITextField!
    var content = ""

@IBAction func segueButton(_ sender: Any) {
        content = textField.text!
        performSegue(withIdentifier: "segue", sender: sender)
    }

Blue screen code

We will write the code inside the Unwid segue. The contents are as stated below.

code

Unwid segue will bring you back to blue segue. Prepare a constant to access the ThirdViewController in it. Since it returns from ThirdViewController, ThirdViewController seems to be the source. Since segue.source is of UIViewController type at this stage, it is necessary to convert it to ThirdViewController type. Let's convert with as? ThirdViewController.

class ViewController: UIViewController {

    @IBOutlet var label: UILabel!
   
    @IBAction func bluesegue(segue: UIStoryboardSegue) {

        let thirdView = segue.source as? ThirdViewController  //You can now access the ThirdViewController.
        label.text = thirdView?.content  //Put the content in the text of the label.
    }
}

I tried to move

ダウンロード (1).gif

Recommended Posts

[Swift] How to use Unwind segue
[Swift] How to use UserDefaults
How to use Swift UIScrollView
[Swift] How to use SwiftLint (cocoapods)
[Swift] How to use Tab Bar Controller
[Swift] How to use one option alert
How to use Map
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use JUnit 5
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
Note how to use Swift super basic TableView
How to use Chain API
[Java] How to use Map
How to use Priority Queuing
[Rails] How to use enum
How to use java Optional
How to use JUnit (beginner)
How to use Ruby return
[Rails] How to use enum
How to use @Builder (Lombok)
How to use java class
How to use Big Decimal
How to use String [] args
[Java] How to use string.format
How to use rails join
How to use Java Map
Ruby: How to use cookies
How to use dependent :: destroy
How to use Eclipse Debug_Shell
How to use Apache POI
[Rails] How to use validation
How to use Java variables
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
How to use GC Viewer
[Java] How to use Optional ①
How to use Lombok now
[Creating] How to use JUnit
[Rails] How to use Scope
How to use the link_to method
How to use arrays (personal memorandum)
How to use scope (JSP & Servlet)
How to use the include? method
[Rails] How to use devise (Note)
How to use the form_with method