Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am trying to add a custom UIViewController class (UIPickerView) to my main ViewController programmatically in Swift (without using a storyboard) but I get the following error message...

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

Custom 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!

        }




    }

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

like image 368
David Power Avatar asked Jan 20 '17 00:01

David Power


1 Answers

addSubview(habitPicker)

The expected argument is UIView, so you can simple fix it by addSubview(habitPicker.view). And remember to adjust habitPicker.view frame size suitable.

like image 117
nynohu Avatar answered Oct 17 '22 22:10

nynohu