2016-04-26 9 views
0

私はアプリケーションを作成し、NSInvalidArgumentExceptionを受信して​​います。このエラーは、iOSエミュレータでアプリケーションを実行し、UISegmentedControlの項目の1つをクリックすると発生します。NSInvalidArgumentException UISegmentedControlの使用

私はそれが私の "mapTypeChanged" 方法に関する信じるが、私は確認したい:

コード:

import UIKit 
import MapKit 

class MapViewController : UIViewController { 

    var mapView: MKMapView! 

    //called if the view property of a ViewController is nill 
    override func loadView() { 
     //Create a map view 

     mapView = MKMapView() 

     //Set it as the "View" of the ViewController 
     view = mapView 

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

     segmentedControl.addTarget(self, action: "mapTypeChanged:", forControlEvents: .ValueChanged) 

     segmentedControl.translatesAutoresizingMaskIntoConstraints = false 
     view.addSubview(segmentedControl) 

     let topConstraint = segmentedControl.topAnchor.constraintEqualToAnchor(topLayoutGuide.bottomAnchor, constant: 8) 
     let margins = view.layoutMarginsGuide 
     let leadingConstraint = segmentedControl.leadingAnchor.constraintEqualToAnchor(margins.leadingAnchor) 
     let trailingConstraint = segmentedControl.trailingAnchor.constraintEqualToAnchor(margins.trailingAnchor) 

     topConstraint.active = true 
     leadingConstraint.active = true 
     trailingConstraint.active = true 

    } 

    func mapTypedChanged(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") 
     } 

    } 

エラー:addTarget

ConversionViewController loaded its view 
MapViewController loaded its view 
2016-04-26 13:54:56.341 WorldTrotter[1530:865418] -[WorldTrotter.MapViewController mapTypeChanged:]: unrecognized selector sent to instance 0x7fb883c8dce0 
2016-04-26 13:54:56.344 WorldTrotter[1530:865418] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[WorldTrotter.MapViewController mapTypeChanged:]: unrecognized selector sent to instance 0x7fb883c8dce0' 
*** First throw call stack: 
(
    0 CoreFoundation      0x0000000102281f45 __exceptionPreprocess + 165 
    1 libobjc.A.dylib      0x0000000103fa5deb objc_exception_throw + 48 
    2 CoreFoundation      0x000000010228a56d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205 
    3 CoreFoundation      0x00000001021d7eea ___forwarding___ + 970 
    4 CoreFoundation      0x00000001021d7a98 _CF_forwarding_prep_0 + 120 
    5 UIKit        0x0000000102a9fe91 -[UIApplication sendAction:to:from:forEvent:] + 92 
    6 UIKit        0x0000000102c0b4d8 -[UIControl sendAction:to:forEvent:] + 67 
    7 UIKit        0x0000000102c0b7a4 -[UIControl _sendActionsForEvents:withEvent:] + 311 
    8 UIKit        0x0000000102cb8661 -[UISegmentedControl _setSelectedSegmentIndex:notify:animate:] + 690 
    9 UIKit        0x0000000102cbad35 -[UISegmentedControl touchesEnded:withEvent:] + 232 
    10 UIKit        0x0000000102b0ded1 -[UIWindow _sendTouchesForEvent:] + 835 
    11 UIKit        0x0000000102b0ec06 -[UIWindow sendEvent:] + 865 
    12 UIKit        0x0000000102abe2fa -[UIApplication sendEvent:] + 263 
    13 UIKit        0x0000000102a98abf _UIApplicationHandleEventQueue + 6844 
    14 CoreFoundation      0x00000001021ae011 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 
    15 CoreFoundation      0x00000001021a3f3c __CFRunLoopDoSources0 + 556 
    16 CoreFoundation      0x00000001021a33f3 __CFRunLoopRun + 867 
    17 CoreFoundation      0x00000001021a2e08 CFRunLoopRunSpecific + 488 
    18 GraphicsServices     0x0000000106b11ad2 GSEventRunModal + 161 
    19 UIKit        0x0000000102a9e30d UIApplicationMain + 171 
    20 WorldTrotter      0x0000000101debdcd main + 109 
    21 libdyld.dylib      0x00000001073e592d start + 1 
) 
libc++abi.dylib: terminating with uncaught exception of type NSException 
(lldb) 
+0

セレクタを使用するには正しいようだが、あなたはスウィフト2.2に '#のselector'を使うべきではないのですか? – Larme

+2

Typo:mapTypeChanged!= mapTypedChanged –

+0

@Larmeが指摘したように、Swift 2.2では構文が変更されています。さらに、新しい '#selector'構文はこれをコンパイル時エラーとして捕捉します。 –

答えて

1

あなたがmapTypeChangedを書いたが、関数はmapTypedChangedと呼ばれます(「型」と「型付き」の区別に注意してください)。代わりに

func mapTypedChanged(segControl: UISegmentedControl)

、正しい

func mapTypeChanged(segControl: UISegmentedControl)

関連する問題