通过分段控制更改地图类型(混合,卫星) [英] Change map type (hybrid, satellite) via segmented control

查看:114
本文介绍了通过分段控制更改地图类型(混合,卫星)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用分段控制按钮来更改地图类型,希望它使用3个选项来更改地图类型:标准,卫星和混合.到目前为止,我已经有了这段代码,但是一旦选择了其他地图类型,则什么也不会发生:

@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
    }
}

我是Swift和Xcode的新手,所以非常感谢您的帮助:)

谢谢

解决方案

首先,请确保在分段控件选择更改时正在调用您的方法.忘记连接出口方法很容易.验证完这一点后,请记住地图数据是异步加载的,因此选择其他模式后可能不会立即看到它的变化.但是,使用您发布的代码,您将从不看到.Hybrid类型,因为三段控件中的selectedSegmentIndex从不为3.

实现此方法的一种更简洁的方法是:

@IBAction func segmentedControlAction(sender: UISegmentedControl!) {
    switch (sender.selectedSegmentIndex) {
        case 0:
            mapView.mapType = .Standard
        case 1:
            mapView.mapType = .Satellite
        default:
            mapView.mapType = .Hybrid
    }
}

请注意,Swift在每个case的末尾都不需要break语句.

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
        }
    }

I am trying to change the map type using a segmented control button, I wish for it to change the type of the map with 3 options: Standard, Satellite and Hybrid. So far I have this code but nothing happens once a different map type is selected:

@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 any help is appreciated :)

Thanks

解决方案

First, ensure that your method is being called when the segmented control selection changes. It's easy to forget to hook up outlet methods. Once you've verified that, remember that map data is loaded asynchronously, so you may not see it change immediately after selecting a different mode. However, with the code you posted, you'll never see the .Hybrid type because the selectedSegmentIndex in a 3-segment control will never be 3.

A more concise way of implementing this method is:

@IBAction func segmentedControlAction(sender: UISegmentedControl!) {
    switch (sender.selectedSegmentIndex) {
        case 0:
            mapView.mapType = .Standard
        case 1:
            mapView.mapType = .Satellite
        default:
            mapView.mapType = .Hybrid
    }
}

Note that Swift doesn't need break statements 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
        }
    }

这篇关于通过分段控制更改地图类型(混合,卫星)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆