[類型 '的MapViewController' 沒有成員 'mapTypeChanged'] [2] 管線:類型 '的MapViewController' 沒有成員 'mapTypeChanged' 夫特大書呆子農場指南
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.")
}
}
請將您的問題複製並粘貼爲文本到您的問題的實際代碼。請確保它的格式正確。 – rmaddy
我編輯了我的文章 - 但是,一些代碼被剪掉了,我無法正確格式化。任何幫助將是真棒,謝謝! @rmaddy – didya26
編輯您的帖子,選擇所有代碼,輸入Ctrl-K將其縮進。 – rmaddy