[类型“MapViewController”没有成员“mapTypeChanged”][2] 在线:
action: #selector(MapViewController.mapTypeChanged(_:)),
代码底部有一个方法 mapTypeChanged
,所以我不确定为什么错误说没有成员 mapTypeChanged
?我猜 mapTypeChanged 需要声明为变量(全局?)
import UIKit
import MapKit
class MapViewController: UIViewController {
var mapView: MKMapView!
override func loadView() {
mapView = MKMapView()
view = mapView
let segmentedControl
= UISegmentedControl(items: ["Standard", "Hybrid", "Satellite"])
segmentedControl.backgroundColor
= UIColor.white.withAlphaComponent(0.5)
segmentedControl.selectedSegmentIndex = 0
segmentedControl.addTarget(self,
action: #selector(MapViewController.mapTypeChanged(_:)),
for: .valueChanged)
segmentedControl.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(segmentedControl)
let topConstraint
= segmentedControl.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor, constant: 8)
let margins = view.layoutMarginsGuide
let leadingConstraint =
segmentedControl.leadingAnchor.constraint(equalTo: margins.leadingAnchor)
let trailingConstraint =
segmentedControl.trailingAnchor.constraint(equalTo: margins.trailingAnchor)
topConstraint.isActive = true
leadingConstraint.isActive = true
trailingConstraint.isActive = true
func mapTypeChanged(_segControl: UISegmentedControl) {
switch _segControl.selectedSegmentIndex {
case 0:
mapView.mapType = .standard
case 1:
mapView.mapType = .hybrid
case 2:
mapView.mapType = .satellite
default:
break
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
print("MapViewController Loaded its view.")
}
}
最佳答案
#selector(MapViewController.mapTypeChanged(_:))
表示“调用 mapTypeChanged
方法”。
所以你需要实现mapTypeChanged
。
func mapTypeChanged(_ sender: UISegmentedControl) {
print(sender.selectedSegmentIndex)
}
关于ios - 类型 'MapViewController' 没有成员 'mapTypeChanged' Swift Big Nerd Ranch Guide,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46165112/