I'm newer to swift and learning about the Delegate communication pattern to pass data between controllers. I'm trying to keep it simple and just want to change the background color and the text of a label.
I was able to set a protocol in my main view controller.
protocol MyViewControllerlDelegate: AnyObject {
func changeLabelName (title: String)
func changeBackgroundColor (color: UIColor)
}
class MyViewController: UIViewController {
weak var viewControllerDelegate: MyViewControllerlDelegate?
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func toNextView(_ sender: UIButton) {
viewControllerDelegate?.changeLabelName(title: "HELLO THERE")
viewControllerDelegate?.changeBackgroundColor(color: .systemCyan)
let secondVC = storyboard?.instantiateViewController(withIdentifier: "second") as! SecondViewController
present(secondVC, animated: true)
}
}
In my second View controller I conform to the MyViewControllerDelegate and set the delegate.
class SecondViewController: UIViewController, MyViewControllerlDelegate {
@IBOutlet var myLabel: UILabel!
var myTitle: String?
var myMainView = MyViewController()
override func viewDidLoad() {
super.viewDidLoad()
myMainView.viewControllerDelegate = self
}
func changeLabelName(title: String) {
myLabel.text = title
}
func changeBackgroundColor(color: UIColor) {
view.backgroundColor = color
}
}
Whenever my second controller is shown the view is white and my label stays the same. I've tried following a few tutorials online and looking at similar problems on here, but I can't figure out why my Delegate methods are not executing on my second view controller.
I've noticed that if I call the methods in the viewDidLoad
it works. Can someone explain what is going wrong?
it's working because you create an object
of a class
while doing it viewDidLoad
will be called.
class SecondViewController: UIViewController, MyViewControllerlDelegate {
........
var myMainView = MyViewController()
........
}
This is not appropriate use of delegate
@HangarRash mentioned in comment.
need to pass data simply in forward data passing scenario. because delegates work in backward data passing.
you can do it like this:
class MyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func toNextView(_ sender: UIButton) {
let secondVC = storyboard?.instantiateViewController(withIdentifier: "second") as! SecondViewController
secondVC.viewBgColor = .systemCyan
secondVC.titleText = "HELLO THERE"
present(secondVC, animated: true)
}
}
class SecondViewController: UIViewController {
@IBOutlet var myLabel: UILabel!
var viewBgColor: UIColor?
var titleText: String?
override func viewDidLoad() {
super.viewDidLoad()
changeLabelName(title: titleText)
changeBackgroundColor(color: viewBgColor)
}
func changeLabelName(title: String) {
myLabel.text = title
}
func changeBackgroundColor(color: UIColor) {
view.backgroundColor = color
}
}