ios - 类型 'MapViewController' 没有成员 'mapTypeChanged' Swift Big Nerd Ranch Guide

标签 ios swift

[类型“MapViewController”没有成员“mapTypeChanged”][2] 在线:

action: #selector(MapViewController.mapTypeChanged(_:)),

代码底部有一个方法 mapTypeChanged ,所以我不确定为什么错误说没有成员 mapTypeChanged ?我猜 mapTypeChanged 需要声明为变量(全局?)

import UIKit
import MapKit


class MapViewController: UIViewController {

    var mapView: MKMapView!

    override func loadView() {

        mapView = MKMapView()
        view = mapView

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

        segmentedControl.addTarget(self,
                                   action: #selector(MapViewController.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() {
        super.viewDidLoad()

        print("MapViewController Loaded its view.")

    }

}

最佳答案

#selector(MapViewController.mapTypeChanged(_:))

表示“调用 mapTypeChanged 方法”。

所以你需要实现mapTypeChanged

func mapTypeChanged(_ sender: UISegmentedControl) {
    print(sender.selectedSegmentIndex)
}

关于ios - 类型 'MapViewController' 没有成员 'mapTypeChanged' Swift Big Nerd Ranch Guide,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46165112/

相关文章:

javascript - 如何重新定位固定的页眉和页脚?

ios - 为什么我的代码返回 null?

ios - 迁移到统一 API 后的错误消息

ios - 如何在IOS中使用 "* "字符进行调用(拨号)?

ios - 通过代码定位 Storyboard View

swift - 我的 "addChild"无法正常工作 Swift

android - 升级 flutter 版本 2.0 时出错

objective-c - 从 Swift 访问用 Objective C 编写的枚举

ios - 针对特定集合的多对多关系核心数据查询

ios - 从主应用程序中,框架如何获得自己的包? (可本地化的字符串)