title: Question about delegate and protocol in swift (with example)
body: <p>I am trying to work with delegate and protocol but met a problem.</p>
  
  <p>I created 2 buttons on one ViewController and created 1 imageView in another ShowViewController. The color of the imageView will change according to which button is pressed.</p>
  
  <p>ViewController.swift</p>
  
  <pre><code>import UIKit
  
  protocol getColorProtocol {
      func getColor(color:String?)
  }
  
  
  class ViewController: UIViewController {
  
      var color: String?
  
      var delegate:getColorProtocol?
  
      @IBAction func blueButtonPressed(_ sender: Any) {
  
          color = "blue"
          delegate?.getColor(color: color)
      }
  
      @IBAction func redButtonPressed(_ sender: Any) {
  
          color = "red"
          delegate?.getColor(color: color)
  
      }
  
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
      }
  
  
  
  }
  </code></pre>
  
  <p>ShowViewController.swift</p>
  
  <pre><code>import UIKit
  
  
  class ShowViewController: UIViewController, getColorProtocol {
  
      var viewOne = ViewController()
  
      @IBOutlet weak var colorView: UIImageView!
  
      func getColor(color: String?) {
          print("color is \(color!)")
  
          if color == "red"{
              colorView.backgroundColor = UIColor(displayP3Red: 255/255, green: 0/255, blue: 0/255, alpha: 1)
          }
          else if color == "blue" {
              colorView.backgroundColor = UIColor(displayP3Red: 0/255, green: 0/255, blue: 255/255, alpha: 1)
  
          }
  
  
      }
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          viewOne.delegate = self
  
      }
  
  }
  </code></pre>
  
  <p><a href="https://imgur.com/a/XQZPHmr" rel="nofollow noreferrer">press here to view the storyboard</a>!</p>
  
  <p>You may check my storyboard above.</p>
  
  <p>Currently, it has no error but it cannot call the getColor function. I am wondering if the problem is coming from the <code>delegate?.getColor(color: color)</code> statement.</p>
  
  <p>Any ideas?</p>