以编程方式将自定义UIViewcontroller添加到子视图,但收到错误消息“无法转换类型的值...”

时间:2017-01-20 00:35:50

标签: ios swift uipickerview

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

  

“无法将'HabitViewViewController'类型的值转换为预期的参数类型'UIView'

自定义UIPicker类:​​


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

1 个答案:

答案 0 :(得分:4)

  

addSubview(habitPicker)

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