[Swift] Problem mit Segmented Controls

Thaddäus

Golden Noble
Registriert
27.03.08
Beiträge
18.368
Hallo Leute

Ich habe hier ein merkwürdiges Problem mit Segmented Controls:

Die App zeigt grundsätzlich eine Map (Google Maps), bei welcher ich den MapType über eben diese Segmented Control steuern will. Hier mal mein Code:

Code:
@IBAction func segmentedControlAction(sender: UISegmentedControl!) {

        switch (sender.selectedSegmentIndex) {

        case 0:

            mapView.mapType = .normal

        case 1:

            mapView.mapType = .satellite

        case 2:

            mapView.mapType = .hybrid

        case 3:

            do {

                // Set the map style by passing the URL of the local file.

                if let styleURL = Bundle.main.url(forResource: "night", withExtension: "json") {

                    mapView.mapStyle = try GMSMapStyle(contentsOfFileURL: styleURL)

                  

                } else {

                    NSLog("Unable to find night.json")

                }

            } catch {

                NSLog("One or more of the map styles failed to load. \(error)")

            }

        default:

            mapView.mapType = .hybrid

        }

    }

Wenn ich die App nun starte, dann sehe ich die Seg Control oben wie im Interface Builder angelegt, und wenn ich "Normal", also Case 0 antippe, dann bekomme ich auch eben diese Ansicht. Aber sobald ich Case 3, also "Night" anwähle, dann wird mir zwar auch der Nachtmodus angezeigt, aber die Kontrollen geraten völlig durcheinander. Sprich, wenn ich nun erneut "Normal" Antippe bekomme ich ebenfalls den Nachtmodus zu sehen, genauso wie wenn ich "Night" antippe.

Satellite und Hybrid funtkionieren derweil tadellos.

Kann mir da vielleicht jemand auf die Sprünge helfen, was da in meinem Code falsch läuft?

Beste Grüsse,

Thaddäus
 
Zuletzt bearbeitet:

Thaddäus

Golden Noble
Registriert
27.03.08
Beiträge
18.368
So, ich konnte das Problem lösen:

Code:
// Paste the JSON string to use.

private let kMapStyle = "all"

Code:
        //Set up map.
        
        let mainBundle = Bundle.main
        let styleUrl: URL? = mainBundle.url(forResource: "night", withExtension: "json")
        // Set the map style by passing the URL for style.json.
        let style = try? GMSMapStyle(contentsOfFileURL: styleUrl!)
        if !(style != nil) {
            print("The style definition could not be loaded")
        }
        mapView.isMyLocationEnabled = false
        mapView.delegate = self
        mapView.settings.rotateGestures = false
        mapView.settings.tiltGestures = false
        mapView.mapType = .hybrid
        mapView.mapStyle = style

Und dann am Ende nur noch die MapTypes für das jeweilige Segment in der Segmented Control festgelegt:

Code:
  // Function for the segmented controlled map type selector
    
    @IBAction func segmentedControlAction(sender: UISegmentedControl!) {
        switch (sender.selectedSegmentIndex) {
        case 0:
            mapView.mapType = .normal
        case 1:
            mapView.mapType = .satellite
        case 2:
            mapView.mapType = .hybrid
        case 3:
            mapView.mapType = .terrain
        default:
            mapView.mapType = .hybrid
        }
    }