@IBAction func segmentedControlAction(sender: UISegmentedControl!) {
if sender .selectedSegmentIndex == 0{
mapView.mapType = MKMapType.Standard
}
else if sender.selectedSegmentIndex == 1{
mapView.mapType = MKMapType.Satellite
}
else if sender.selectedSegmentIndex == 3{
mapView.mapType = MKMapType.Hybrid
}
}
I am new to Swift and Xcode so I appreciate any help:)
Thanks
Implementation A more concise way of this method is:
@IBAction func segmentedControlAction(sender: UISegmentedControl!) {
switch (sender.selectedSegmentIndex) {
c ase 0:
mapView.mapType = .Standard
case 1:
mapView.mapType = .Satellite
default:
mapView.mapType = .Hybrid
}
}
Please note that Swift does not need a break statement at the end of each case.
Edit: Swift 4.1
@IBAction func mapTypeSegmentSelected(_ sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 0:
mapView.mapType = .normal
case 1:
mapView.mapType = .satellite
default:
mapView.mapType = .hybrid
}
}
Me I am trying to use the segment control button to change the map type, I hope it can use 3 options to change the map type: standard, satellite and hybrid. So far, I have this code but once a different map type is selected, nothing happens:
@IBAction func segmentedControlAction(sender: UISegmentedControl!) {
if sender.selectedSegmentIndex == 0{
mapView.mapType = MKMapType.Standard
}
else if sender.selectedSegmentIndex == 1{
mapView.mapType = MKMapType.Satellite
}
else if sender.selectedSegmentIndex == 3{
mapView.mapType = MKMapType.Hybrid
}
}
I am new to Swift and Xcode so I appreciate any help:)
Thank you
First of all, make sure to The method is called when the segmented control selection changes. It is easy to forget the connection socket method. Once this is confirmed, remember that the map data is loaded asynchronously, so you may not see it change immediately after selecting another mode. However, Using the code you posted, you will never see the .Hybrid type, because the selectedSegmentIndex in the 3-segment control will never be 3.
The more concise way to implement this method is:< /p>
@IBAction func segmentedControlAction(sender: UISegmentedControl!) {
switch (sender.selectedSegmentIndex) {
case 0:
mapView.mapType = .Standard
case 1:
mapView.mapType = .Satellite
default:
mapView.mapType = .Hybrid
}
}
< p>Please note that Swift does not need a break statement at the end of each case.
Edit: Swift 4.1
@IBAction func mapTypeSegmentSelected(_ sender : UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 0:
mapView .mapType = .normal
case 1:
mapView.mapType = .satellite
default:
mapView.mapType = .hybrid
}
} pre>