title: How do I change the background color of the text in view of the UIPickerView with multiple components when a valid selection is made, in Swift
body: <p>Below code will change the background color of the UIPickerView to cyan showing it is a valid selection when total of row indexes = 6. Would like to have pickerLabel background in view(selected) to turn cyan instead in all four components when selection is valid. Any idea?</p>
  
  <pre><code>import UIKit
  
  class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
  
      @IBOutlet weak var myPicker: UIPickerView!
      let pickerData0 = ["0R","1R","2R","3R","4R","5R","6R"]
      let pickerData1 = ["0B","1B","2B","3B","4B","5B","6B"]
      let pickerData2 = ["0Y","1Y","2Y","3Y","4Y","5Y","6Y"]
      let pickerData3 = ["0G","1G","2G","3G","4G","5G","6G"]
  
      override func viewDidLoad() {
          super.viewDidLoad()
          // Do any additional setup after loading the view, typically from a nib.
          myPicker.dataSource = self
          myPicker.delegate = self
  
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  
  
      //Mark: Delegates and data sources
      //Mark: Data Sources
      func numberOfComponentsInPickerView(pickerView: UIPickerView) -&gt; Int {
          return 4
      }
  
      func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -&gt; Int {
          return pickerData0.count
      }
      //Mark: Delegates
  
      func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
          let redIndex = myPicker.selectedRowInComponent(0)
          let blueIndex = myPicker.selectedRowInComponent(1)
          let yellowIndex = myPicker.selectedRowInComponent(2)
          let greenIndex = myPicker.selectedRowInComponent(3)
  
          if redIndex + blueIndex + yellowIndex + greenIndex == 6 {
              myPicker.backgroundColor = UIColor.cyanColor()
          } else {
              myPicker.backgroundColor = UIColor.whiteColor()
          }   
      }
  
  
      func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -&gt; UIView {
  
          switch component {
          case 0:
              var pickerLabel = view as! UILabel!
              if view == nil {
                  pickerLabel = UILabel()
                  pickerLabel.textColor = UIColor.redColor()
                  pickerLabel.backgroundColor = UIColor.grayColor()
                  pickerLabel.textAlignment = NSTextAlignment.Center
                  pickerLabel.text = pickerData0[row]
                  pickerLabel.font = UIFont(name: "Georgia", size: 26)
              }
              return pickerLabel
          case 1:
              var pickerLabel = view as! UILabel!
              if view == nil {
                  pickerLabel = UILabel()
                  pickerLabel.textColor = UIColor.blueColor()
                  pickerLabel.backgroundColor = UIColor.grayColor()
                  pickerLabel.textAlignment = NSTextAlignment.Center
                  pickerLabel.text = pickerData1[row]
                  pickerLabel.font = UIFont(name: "Georgia", size: 26)
              }
              return pickerLabel
          case 2:
              var pickerLabel = view as! UILabel!
              if view == nil {
                  pickerLabel = UILabel()
                  pickerLabel.textColor = UIColor.yellowColor()
                  pickerLabel.backgroundColor = UIColor.grayColor()
                  pickerLabel.textAlignment = NSTextAlignment.Center
                  pickerLabel.text = pickerData2[row]
                  pickerLabel.font = UIFont(name: "Georgia", size: 26)
              }
              return pickerLabel
          case 3:
              var pickerLabel = view as! UILabel!
              if view == nil {
                  pickerLabel = UILabel()
                  pickerLabel.textColor = UIColor.greenColor()
                  pickerLabel.backgroundColor = UIColor.grayColor()
                  pickerLabel.textAlignment = NSTextAlignment.Center
                  pickerLabel.text = pickerData3[row]
                  pickerLabel.font = UIFont(name: "Georgia", size: 26)
              }
              return pickerLabel
          default:
            return pickerView
          }
      }
  }
  </code></pre>