我已經閱讀了關於此主題的以前的文章,並且我確保我的代碼不包含相同的錯誤,但當我嘗試點擊時,我一直收到錯誤「無法識別的選擇器發送到實例」我的UIButton。任何人都可以弄清楚是什麼問題?我確定我的動作名稱和簽名與我連接到我的按鈕的功能完全相同。我試過重新啓動XCode,但它仍然無法工作。任何輸入讚賞。無法識別的選擇器swift
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.2,你應該使用#selector。您是否可以發佈您的真實(和完整)錯誤消息,以防萬一? – Larme