I'm trying to pass data from a ViewController (let's call it ViewControllerA) to a ViewControllerB. I created the ViewControllers programmatically so I am not using the storyboard at all. I would like to display in a UILabel in the ViewControllerB what the user types in a UITextField in ViewControllerA. Is there any way I can do this programmatically? I'm just a beginner so give me the simplest solution you know because it is the easiest one to grasp for me. Thank you!
Think is a sample of the code
ViewControllerA:
import UIKit
class ViewControllerA: UIViewController {
let player1: UITextField = {
let p1 = UITextField()
p1.placeholder = "Player"
return p1
}()
let goButton: UIButton = {
let go = UIButton(type: .system)
go.setTitle("Go!", for: .normal)
go.addTarget(self, action: #selector(Go), for: .touchUpInside)
return go
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(player1)
view.addSubview(goButton)
}
func Go() {
let viewB = ViewControllerB()
present(viewB, animated: true, completion: nil)
}
}
ViewControllerB :
import UIKit
class ViewControllerB: UIViewController {
let topLabel: UILabel = {
let tl = UILabel()
tl.textColor = UIColor.white
return tl
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(topLabel)
}
}
(When the user presses the goButton, I'd like the topLabel.text in ViewControllerB to be equal to the player1.text of ViewControllerA)