Overview: When I click "Choose Color" button in the 1st view controller, it will direct me to the 2nd view controller to choose red or blue background color by clicking "Red" or "Blue" button. At last, the 2nd view controller will be dismissed after clicking the color button and background color will be display on the 1st view controller.
Problem: background color can not be displayed on the 1st view controller.
My code for 1st view controller:
import UIKit
protocol ChangingColor{
func changingColor(color: UIColor)
}
class BossVC: UIViewController {
@IBOutlet weak var redButton: UIButton!
var BossDelegate: ChangingColor?
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func redTapped(_ sender: UIButton) {
BossDelegate?.changingColor(color: .red)
dismiss(animated: true, completion: nil)
}
@IBAction func blueTapped(_ sender: UIButton) {
BossDelegate?.changingColor(color: .blue)
dismiss(animated: true, completion: nil)
}
}
My code for 2nd view controller:
import UIKit
class FreelancerVC: UIViewController, ChangingColor{
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func handleChooseColorTapped(_ sender: UIButton) {
performSegue(withIdentifier: "goToBoss", sender: self)
let bossVC = BossVC()
bossVC.BossDelegate = self
}
func changingColor(color: UIColor) {
view.backgroundColor = color
}
}