我正在创建一个应用程序并收到NSInvalidArgumentException
。当我在iOS模拟器上运行我的应用程序并单击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 :(得分:1)
在addTarget
中您编写了mapTypeChanged
,但该函数名为mapTypedChanged
(注意“类型”与“已键入”)。
而不是
func mapTypedChanged(segControl: UISegmentedControl)
,使用正确的
func mapTypeChanged(segControl: UISegmentedControl)