Programmatic Controls

Now let’s update the segmented control to change the map type when the user taps on a segment.

A UISegmentedControl is a subclass of UIControl. You worked with another UIControl subclass in Chapter 1: the UIButton class. Controls are responsible for calling methods on their target in response to some event.

Control events are of type UIControl.Event. Here are a few of the common control events that you will use:

UIControl.Event.touchDown

A touch down on the control.

UIControl.Event.touchUpInside

A touch down followed by a touch up while still within the bounds of the control.

UIControl.Event.valueChanged

A touch that causes the value of the control to change.

UIControl.Event.editingChanged

A touch that causes an editing change for a UITextField.

The .touchDown type is rarely used; you used .touchUpInside for the UIButton in Chapter 1 (it is the default event when you Control-drag to connect actions in Interface Builder), and you will see the .editingChanged event in Chapter 6. For the segmented control, you will use the .valueChanged event.

In MapViewController.swift, update loadView() to add a target-action pair to the segmented control and associate it with the .valueChanged event.

Listing 5.7  Attaching a target-action pair to the segmented control (MapViewController.swift)

let segmentedControl
        = UISegmentedControl(items: ["Standard", "Satellite", "Hybrid"])
segmentedControl.backgroundColor = UIColor.systemBackground
segmentedControl.selectedSegmentIndex = 0

segmentedControl.addTarget(self,
                           action: #selector(mapTypeChanged(_:)),
                           for: .valueChanged)

Next, implement the action method in MapViewController that the event will trigger. This method will check which segment was selected and update the map accordingly.

(In the code above – and previously throughout this book – we included existing code so that you could position new code correctly. In the code below, we do not provide that context because the position of the new code is not important so long as it is within the curly braces for the type being implemented – in this case, the MapViewController class. When a code block includes all new code, like this one, we suggest that you put it at the end of the type’s implementation, just inside the final closing brace. In Chapter 15, you will see how to easily navigate within an implementation file when your files get longer and more complex.)

Listing 5.8  Implementing the mapTypeChanged(_:) action (MapViewController.swift)

@objc func mapTypeChanged(_ segControl: UISegmentedControl) {
    switch segControl.selectedSegmentIndex {
    case 0:
        mapView.mapType = .standard
    case 1:
        mapView.mapType = .hybrid
    case 2:
        mapView.mapType = .satellite
    default:
        break
    }
}

The @objc annotation is needed to expose this method to the Objective-C runtime. Recall that many iOS frameworks are still written in Objective-C even though we interact with them through Swift. Without this annotation, the segmented control cannot see this action method.

Build and run the application. Change the selected segment and the map will update.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.6.194