我试图在 Swift
(不使用故事板)中以编程方式向我的主 ViewController
添加自定义 UIViewController
类( UIPickerView
),但是我收到以下错误消息...
“无法将'HabitViewViewController'类型的值转换为预期的参数类型'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")
}
}
1 回答
预期的参数是UIView,所以你可以通过
addSubview(habitPicker.view)
简单地修复它 . 并记得调整habitPicker.view
适合的帧大小 .