title: Adding a custom UIViewcontroller to subview programmatically but getting an error message "Cannot convert value of type..."
body: <p>I am trying to add a custom <code>UIViewController</code> class (<code>UIPickerView</code>) to my main <code>ViewController</code> programmatically in <code>Swift</code> (without using a storyboard) but I get the following error message...</p>
  
  <blockquote>
    <p>"Cannot convert value of type 'HabitViewViewController' to expected argument type 'UIView'</p>
  </blockquote>
  
  <p><strong>Custom UIPicker class:</strong></p>
  
  <pre>
  
      import UIKit
  
      class HabitViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate  {
  
          @IBOutlet weak var myPicker: UIPickerView!
          @IBOutlet weak var myLabel: UILabel!
          let pickerData = ["Mozzarella","Gorgonzola","Provolone","Brie","Maytag Blue","Sharp Cheddar","Monterrey Jack","Stilton","Gouda","Goat Cheese", "Asiago"]
  
          override func viewDidLoad() {
              super.viewDidLoad()
              myPicker.delegate = self
              myPicker.dataSource = self
  
          }
          //MARK: - Delegates and data sources
          //MARK: Data Sources
  
          func numberOfComponents(in pickerView: UIPickerView) -> Int {
              return 1
          }
  
          func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
              return pickerData.count
          }
  
          //MARK: Delegates
  
          func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
              return pickerData[row]
          }
  
          func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
              myLabel.text = pickerData[row]
          }
  
  
          func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
              let titleData = pickerData[row]
              let myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 26.0)!,NSForegroundColorAttributeName:UIColor.blue])
              return myTitle
          }
  
  
          func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
              var pickerLabel = view as! UILabel!
              if view == nil {  //if no label there yet
                  pickerLabel = UILabel()
                  //color the label's background
                  let hue = CGFloat(row)/CGFloat(pickerData.count)
                  pickerLabel?.backgroundColor = UIColor(hue: hue, saturation: 1.0, brightness: 1.0, alpha: 1.0)
              }
              let titleData = pickerData[row]
              let myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 26.0)!,NSForegroundColorAttributeName:UIColor.black])
              pickerLabel!.attributedText = myTitle
              pickerLabel!.textAlignment = .center
  
              return pickerLabel!
  
          }
  
  
  
  
      }
  
  </pre>
  
  <p><strong>Main UIView</strong></p>
  
  <pre>
  
      import UIKit
  
      // Activity Month view Class (Type BaseCell - cleaner)
      class PlantCell: BaseCell {
  
  
          // UIpicker for habit
          let habitPicker: HabitViewController = {
  
              let habit = HabitViewController()
              return habit
          }()
  
  
          // Overrided as it uses the baseCell superclass
          override func setupViews() {
  
              // Add subviews
              addSubview(habitPicker)
  
              // Horizontal constraints
              addConstraintsWithFormat(format: "H:|-[v0]-|", views: habitPicker)
  
              // Vertical constraints
              addConstraintsWithFormat(format: "V:|-250-[v0(20)]", views: habitPicker)
  
  
          }
  
  
      }
  
  </pre>
  
  <p><strong>BaseCell</strong></p>
  
  <pre>
  
      import UIKit
  
      // Superclass to initalise all base UICollectionView cells
      class BaseCell: UICollectionViewCell {
          override init(frame: CGRect) {
              // When dequeueReusableCell is called this init method is called if it needs a new cell
              super.init(frame: frame)
              setupViews()
          }
  
          func setupViews() {
  
          }
  
          required init?(coder aDecoder: NSCoder) {
              fatalError("init(coder:) has not been implemented")
          }
      }
  
  </pre>