I'm new to programming & Swift and I am trying to understand how to pass data between two view controllers (no segue) with protocols and delegates.
I have a View Controller (VIEW A) which has a text field and button. When the user hits that button, it should then show that text in a label in another View Controller (VIEW B).
I cannot get the label to show the text - I would appreciate an explanation of what is required to make this work.
Thanks so much!
import UIKit
protocol sendNameToViewB {
func showNameLabel(name:String)
}
class ViewA: UIViewController {
var delegate: sendNameToViewB?
@IBOutlet weak var textField: UITextField!
@IBAction func addButton(sender: AnyObject) {
delegate?.showNameLabel(textField.text)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
class ViewB: UIViewController, sendNameToViewB {
@IBOutlet weak var theLabel: UILabel!
func showNameLabel(name: String) {
theLabel.text = name
}
}