以编程方式将自定义 UIViewcontroller 添加到子视图但收到错误消息 "Cannot convert value of type..."

Adding a custom UIViewcontroller to subview programmatically but getting an error message "Cannot convert value of type..."

我正在尝试在 Swift 中以编程方式将自定义 UIViewController class (UIPickerView) 添加到我的主 ViewController(不使用故事板)但我收到以下错误消息...

"Cannot convert value of type 'HabitViewViewController' to expected argument type 'UIView'

自定义 UIPicker class:


    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!

        }




    }

主 UIView


    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)


        }


    }

BaseCell


    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")
        }
    }

addSubview(habitPicker)

预期的参数是 UIView,因此您可以通过 addSubview(habitPicker.view) 简单地修复它。并记得调整 habitPicker.view 合适的帧大小。