2016-04-26 55 views
0

我正在創建一個應用程序並收到一個NSInvalidArgumentException。當我在iOS模擬器上運行我的應用程序並單擊UISegmentedControl中的某個項目時,會發生該錯誤。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") 
     } 

    } 

錯誤:

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

addTarget你寫mapTypeChanged,但函數被稱爲mapTypedChanged(通知「類型」與「類型」)。

而不是

func mapTypedChanged(segControl: UISegmentedControl)

,使用正確的

func mapTypeChanged(segControl: UISegmentedControl)