是什么给出的?我一直得到的错误是在action参数的第一行。我已经尝试了“mapTypeChanged:”....#selector(mapTypeChanged)...以及更多..我不断收到黄色或红色错误。怎么了?
class viewControllerForMap: UIViewController {
var mapView : MKMapView!
override func loadView() {
mapView = MKMapView()
self.view = mapView
let segmentedControl = UISegmentedControl(items: ["Standard", "Hybrid", "Satellite"])
segmentedControl.backgroundColor = UIColor.white.withAlphaComponent(0.5)
segmentedControl.selectedSegmentIndex = 0
segmentedControl.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(segmentedControl)
segmentedControl.addTarget(self, action: #selector(mapTypeChanged(_:)), for: .valueChanged)
func mapTypeChanged(_ sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 0:
mapView.mapType = .standard
case 1:
mapView.mapType = .hybrid
case 2:
mapView.mapType = .satellite
case _:
break
}
}
let topConstraint = segmentedControl.topAnchor.constraint(equalTo: self.topLayoutGuide.bottomAnchor, constant: 8)
let leadingConstraint = segmentedControl.leadingAnchor.constraint(equalTo: self.view.layoutMarginsGuide.leadingAnchor)
let trailingConstraint = segmentedControl.trailingAnchor.constraint(equalTo: self.view.layoutMarginsGuide.trailingAnchor)
topConstraint.isActive = true
leadingConstraint.isActive = true
trailingConstraint.isActive = true
}
}
答案 0 :(得分:1)
根据更新的代码,问题似乎是您的
func mapTypeChanged(_ sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 0:
mapView.mapType = .standard
case 1:
mapView.mapType = .hybrid
case 2:
mapView.mapType = .satellite
case _:
break
}
}
功能目前在您的其他方法中。
应该看起来像:
class viewControllerForMap: UIViewController {
var mapView : MKMapView!
override func loadView() {
mapView = MKMapView()
self.view = mapView
let segmentedControl = UISegmentedControl(items: ["Standard", "Hybrid", "Satellite"])
segmentedControl.backgroundColor = UIColor.white.withAlphaComponent(0.5)
segmentedControl.selectedSegmentIndex = 0
segmentedControl.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(segmentedControl)
segmentedControl.addTarget(self, action: #selector(mapTypeChanged(_:)), for: .valueChanged)
let topConstraint = segmentedControl.topAnchor.constraint(equalTo: self.topLayoutGuide.bottomAnchor, constant: 8)
let leadingConstraint = segmentedControl.leadingAnchor.constraint(equalTo: self.view.layoutMarginsGuide.leadingAnchor)
let trailingConstraint = segmentedControl.trailingAnchor.constraint(equalTo: self.view.layoutMarginsGuide.trailingAnchor)
topConstraint.isActive = true
leadingConstraint.isActive = true
trailingConstraint.isActive = true
}
func mapTypeChanged(_ sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 0:
mapView.mapType = .standard
case 1:
mapView.mapType = .hybrid
case 2:
mapView.mapType = .satellite
case _:
break
}
}
}
希望有所帮助。