07-18-2023, 10:51 PM
# Swift
There are tons and tons of explanations here and around Stack Overflow, but if you are a beginner just trying to get something basic to work, try watching this YouTube tutorial (It's what helped me to finally understand how to do it).
- YouTube tutorial: *[How to send data through segue (Swift)][1]*
# Passing data forward to the next View Controller
The following is an example based on the video. The idea is to pass a string from the text field in the First View Controller to the label in the Second View Controller.
[![Enter image description here][2]][2]
Create the storyboard layout in the Interface Builder. To make the segue, you just <kbd>Control</kbd> click on the button and drag over to the Second View Controller.
**First View Controller**
The code for the First View Controller is
import UIKit
class FirstViewController: UIViewController {
@IBOutlet weak var textField: UITextField!
// This function is called before the segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get a reference to the second view controller
let secondViewController = segue.destination as! SecondViewController
// Set a variable in the second view controller with the String to pass
secondViewController.receivedString = textField.text!
}
}
**Second View Controller**
And the code for the Second View Controller is
import UIKit
class SecondViewController: UIViewController {
@IBOutlet weak var label: UILabel!
// This variable will hold the data being passed from the First View Controller
var receivedString = ""
override func viewDidLoad() {
super.viewDidLoad()
// Used the text from the First View Controller to set the label
label.text = receivedString
}
}
**Don't forget**
- Hook up the outlets for the `UITextField` and the `UILabel`.
- Set the first and second View Controllers to the appropriate Swift files in [Interface Builder][3].
# Passing data back to the previous View Controller
To pass data back from the second view controller to the first view controller, you use [a protocol and a delegate][4]. This video is a very clear walk though of that process:
- YouTube tutorial: [iOS Swift Basics Tutorial: Protocols and Delegates][5] But also read [this post][6] to make sure you don't get into a strong reference cycle.
The following is an example based on the video (with a few modifications).
[![Enter image description here][7]][7]
Create the storyboard layout in the Interface Builder. Again, to make the segue, you just <kbd>Control</kbd> drag from the button to the Second View Controller. Set the segue identifier to `showSecondViewController`. Also, don't forget to hook up the outlets and actions using the names in the following code.
**First View Controller**
The code for the First View Controller is
import UIKit
class FirstViewController: UIViewController, DataEnteredDelegate {
@IBOutlet weak var label: UILabel!
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showSecondViewController" {
let secondViewController = segue.destination as! SecondViewController
secondViewController.delegate = self
}
}
func userDidEnterInformation(info: String) {
label.text = info
}
}
Note the use of our custom `DataEnteredDelegate` protocol.
**Second View Controller and Protocol**
The code for the second view controller is
import UIKit
// Protocol used for sending data back
protocol DataEnteredDelegate: AnyObject {
func userDidEnterInformation(info: String)
}
class SecondViewController: UIViewController {
// Making this a weak variable, so that it won't create a strong reference cycle
weak var delegate: DataEnteredDelegate? = nil
@IBOutlet weak var textField: UITextField!
@IBAction func sendTextBackButton(sender: AnyObject) {
// Call this method on whichever class implements our delegate protocol
delegate?.userDidEnterInformation(info: textField.text!)
// Go back to the previous view controller
_ = self.navigationController?.popViewController(animated: true)
}
}
Note that the `protocol` is outside of the View Controller class.
That's it. Running the app now, you should be able to send data back from the second view controller to the first.
[1]:
[2]:
[3]:
[6]:
There are tons and tons of explanations here and around Stack&nbsp;Overflow, but if you are a beginner just trying to get something basic to work, try watching this YouTube tutorial (It's what helped me to finally understand how to do it).
- YouTube tutorial: *[How to send data through segue (Swift)][1]*
# Passing data forward to the next View Controller
The following is an example based on the video. The idea is to pass a string from the text field in the First View Controller to the label in the Second View Controller.
[![Enter image description here][2]][2]
Create the storyboard layout in the Interface Builder. To make the segue, you just <kbd>Control</kbd> click on the button and drag over to the Second View Controller.
**First View Controller**
The code for the First View Controller is
import UIKit
class FirstViewController: UIViewController {
@IBOutlet weak var textField: UITextField!
// This function is called before the segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get a reference to the second view controller
let secondViewController = segue.destination as! SecondViewController
// Set a variable in the second view controller with the String to pass
secondViewController.receivedString = textField.text!
}
}
**Second View Controller**
And the code for the Second View Controller is
import UIKit
class SecondViewController: UIViewController {
@IBOutlet weak var label: UILabel!
// This variable will hold the data being passed from the First View Controller
var receivedString = ""
override func viewDidLoad() {
super.viewDidLoad()
// Used the text from the First View Controller to set the label
label.text = receivedString
}
}
**Don't forget**
- Hook up the outlets for the `UITextField` and the `UILabel`.
- Set the first and second View Controllers to the appropriate Swift files in [Interface Builder][3].
# Passing data back to the previous View Controller
To pass data back from the second view controller to the first view controller, you use [a protocol and a delegate][4]. This video is a very clear walk though of that process:
- YouTube tutorial: [iOS Swift Basics Tutorial: Protocols and Delegates][5] But also read [this post][6] to make sure you don't get into a strong reference cycle.
The following is an example based on the video (with a few modifications).
[![Enter image description here][7]][7]
Create the storyboard layout in the Interface Builder. Again, to make the segue, you just <kbd>Control</kbd> drag from the button to the Second View Controller. Set the segue identifier to `showSecondViewController`. Also, don't forget to hook up the outlets and actions using the names in the following code.
**First View Controller**
The code for the First View Controller is
import UIKit
class FirstViewController: UIViewController, DataEnteredDelegate {
@IBOutlet weak var label: UILabel!
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showSecondViewController" {
let secondViewController = segue.destination as! SecondViewController
secondViewController.delegate = self
}
}
func userDidEnterInformation(info: String) {
label.text = info
}
}
Note the use of our custom `DataEnteredDelegate` protocol.
**Second View Controller and Protocol**
The code for the second view controller is
import UIKit
// Protocol used for sending data back
protocol DataEnteredDelegate: AnyObject {
func userDidEnterInformation(info: String)
}
class SecondViewController: UIViewController {
// Making this a weak variable, so that it won't create a strong reference cycle
weak var delegate: DataEnteredDelegate? = nil
@IBOutlet weak var textField: UITextField!
@IBAction func sendTextBackButton(sender: AnyObject) {
// Call this method on whichever class implements our delegate protocol
delegate?.userDidEnterInformation(info: textField.text!)
// Go back to the previous view controller
_ = self.navigationController?.popViewController(animated: true)
}
}
Note that the `protocol` is outside of the View Controller class.
That's it. Running the app now, you should be able to send data back from the second view controller to the first.
[1]:
[2]:
[3]:
[To see links please register here]
[4]:[To see links please register here]
[5]:[6]:
[To see links please register here]
[7]: