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.
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.
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 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.
The name of the function you set earlier will be displayed, so click it. This completes the linking. Unwind segue has been added as shown in red.
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.
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. Then set the Identifier to Unwind segue. By doing so, you can return with Unwind segue only when you specify the Identifier.
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)
}
We will write the code inside the Unwid segue. The contents are as stated below.
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.
}
}
Recommended Posts