我正在关注 big nerd 分支的一本书,名为“iOS 编程第 5 版”。
这段代码在地图视图中创建了一个分段视图,每次我在运行它时更改分段时,它都会崩溃:
线程 1:信号 SIGABRT
当我使用最新版本时,这可能与书中的旧版本 Swift 有关。
这是我的代码:
class MapViewController: UIViewController
{
var mapView: MKMapView!
override func loadView()
{
//Create a map view
mapView = MKMapView ()
//Set it as *the* view of this view controller
view = mapView
let segmentedControl = UISegmentedControl (items: ["Standard", "Hybrid", "Satelite"])
segmentedControl.backgroundColor = UIColor.white.withAlphaComponent (0.5)
segmentedControl.selectedSegmentIndex = 0
segmentedControl.addTarget(self, action: Selector(("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()
{
// Always call the super implementation of ViewDidLoad
super.viewDidLoad()
print ("MapViewController loaded its view")
}
}
我该如何解决?