2016-05-12 5 views
-2

私はこのトピックに関して以前の記事を熟読しましたが、コードに同じバグが含まれていないことを保証しましたが、タップを試みると「認識できないセレクターがインスタンスに送られました」私の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") 

    } 
} 
+1

注外の関数を宣言します。あなたの本当の(そして完全な)エラーメッセージを投稿できますか? – Larme

答えて

6

あなたのアクションが参照している機能が間違った範囲にあります。単に

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

override func loadView()機能の外に持ち込むだけです。

関数はloadViewの実行中にのみ存在するため、実際にボタンをタップしたときには使用できません。

+0

ああ、完璧です。 @Jamstopの説明をありがとう! –

0

スイフト2.2で、あなたは#selectorを使用する必要があることをごloadView方法

override func loadView() { 
} 

func zoomToUser(sender: UIButton!) { 
      mapView.showsUserLocation = true 
     } 
関連する問題