无法识别的选择器迅速

时间:2016-05-12 18:37:45

标签: ios swift

我已经仔细阅读了有关此主题的SO上的帖子,并且我确保我的代码不包含相同的错误,但我一直收到错误"无法识别的选择器已发送到实例"当我尝试点击我的UIButton。谁能弄明白问题是什么?我确保我的动作名称和签名与我连接到我的按钮的功能相同。我尝试重启XCode但它仍然无法正常工作。任何输入都表示赞赏。

import UIKit
import MapKit

class MapViewController: UIViewController {

    var mapView: MKMapView!

    override func loadView() {

        //create an instance of the MkMapView class and set it as the view controllers view
        mapView = MKMapView ()
        view = mapView

        //create a set of segmented controls to the map interface to give the user some options regarding their map 
        let segmentedControls = UISegmentedControl(items: ["Satellite", "Standard", "Hybrid"])

        //set the color of the segemented controls and set which index they default to on launch
        segmentedControls.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.5)
        segmentedControls.selectedSegmentIndex = 0

        //how auto-layout used to work was each view would have an auto-resizing mask that iOS would look at and add constraints onto the view based on its mask. The problem is now that we can manually add constraints ourselves, we run into conflicts in the layout between the constraints we set out and those iOS sets up itself through the mask. The best way to avoid this is to simply set the translatesAutoreszing... property to "false" so that iOS doesn't create its own constraints and only ours get set in the project
        segmentedControls.translatesAutoresizingMaskIntoConstraints = false

        //add the segmentedControl to the main view
        view.addSubview(segmentedControls)

        //use the view margins to set the insets of the segmented controls- that way they'll adapt to the margins of whatever screen the ap loads on
        let margins = view.layoutMarginsGuide

        //create a set of constraints for the segmented controls
        let topConstraint = segmentedControls.topAnchor.constraintEqualToAnchor(topLayoutGuide.bottomAnchor, constant: 8)
        let leadConstraint = segmentedControls.leadingAnchor.constraintEqualToAnchor(margins.leadingAnchor)
        let traiConstraint = segmentedControls.trailingAnchor.constraintEqualToAnchor(margins.trailingAnchor)

        //activate the constraints
        topConstraint.active = true
        leadConstraint.active = true
        trailConstraint.active = true

        //create a UIButton, set its label, and add it to the view hierarchy
        let button = UIButton(type: .System)
        button.setTitle("Show Location", forState: .Normal)
        button.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(button)

        //create constraints and set them to active
        let buttonBottomConstraint = button.bottomAnchor.constraintEqualToAnchor(bottomLayoutGuide.topAnchor)
        let buttonLeadConstraint = button.leadingAnchor.constraintEqualToAnchor(margins.leadingAnchor)
        let buttonTrailConstraint = button.trailingAnchor.constraintEqualToAnchor(margins.trailingAnchor);

        buttonBottomConstraint.active = true
        buttonLeadConstraint.active = true
        buttonTrailConstraint.active = true

        //set the action-target connection
        button.addTarget(self, action: "zoomToUser:", forControlEvents: UIControlEvents.TouchUpInside)

        func zoomToUser(sender: UIButton!) {
            mapView.showsUserLocation = true
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        print("Loaded map view")

    }
}

2 个答案:

答案 0 :(得分:6)

您的操作引用的函数位于错误的范围内。只需带上

func zoomToUser(sender: UIButton!) {
    mapView.showsUserLocation = true
}

override func loadView()函数之外。

因为您的功能仅在loadView运行时存在,所以当您实际点击按钮时,您无法使用它。

答案 1 :(得分:0)

shadowBlur方法

之外声明函数
loadView