我想要添加子视图时出现此错误。正如你可以选择错误出现的代码。我是 编程相对较新,所以我需要帮助。我正在使用Swift编写而不使用故事板。无法将类型'(MapViewController).Type'(又名'MapViewController.Type')的值转换为期望的参数类型'UIView'
忽略其中我写道:的backgroundColor = UIColor.blueColor()
编辑:我忘了提,我知道是什么原因造成的错误。我知道MapViewController是一个UIViewController,我把它放在UIView中。但我不知道如何制定解决方案。
我要的是一个mapview
添加到单个细胞
import UIKit
import MapKit
class MapCell1: MapCell, MKMapViewDelegate {
override func setupViews() {
backgroundColor = UIColor.blueColor()
addSubview(MapViewController) //Cannot convert value of type '(MapViewController).Type' (aka 'MapViewController.Type') to expected argument type 'UIView'
}
}
import UIKit
import MapKit
class MapViewController: UIViewController {
var window: UIWindow?
var mapView: MKMapView?
override func viewDidLoad() {
super.viewDidLoad()
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.view.backgroundColor = UIColor.whiteColor()
self.mapView = MKMapView(frame: CGRectMake(0, 20, (self.window?.frame.width)!, 300))
self.view.addSubview(self.mapView!)
}
}
这里是MapCell代码:
import UIKit
class MapCell: BaseCell, UICollectionViewDelegate {
let collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.redColor()
return cv
}()
let cellId = "cellId"
override func setupViews() {
super.setupViews()
addSubview(collectionView)
addconstraintsWithFormat("H:|[v0]|", views: collectionView)
addconstraintsWithFormat("V:|[v0]|", views: collectionView)
}
}
And BaseCell:
mport UIKit
class BaseCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
func setupViews() {
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
class BaseCell: UICollectionViewCell {
func setupViews() {
}
}
}
的MapViewController是一个视图控制器.. 。你试图将它添加到视图中...我不确定这是你想要做什么 – penatheboss
是的,这是我正在尝试做的。 – Stc
您无法将视图控制器添加到视图中。您可以将视图添加到视图控制器的视图或视图 – penatheboss