ios - NSInvalidArgumentException 使用 UISegmentedControl

标签 ios iphone swift uisegmentedcontrol

我正在创建一个应用程序并收到一个 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) 

最佳答案

addTarget 中,您编写了 mapTypeChanged,但该函数称为 mapTypedChanged(注意“Type”与“Typed”)。

代替

func mapTypedChanged(segControl: UISegmentedControl)

,使用正确的

func mapTypeChanged(segControl: UISegmentedControl)

关于ios - NSInvalidArgumentException 使用 UISegmentedControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36873755/

相关文章:

ios - 如何为每个表格 View 单元添加一个计时器,该计时器在用户每次滚动时更新?

objective-c - UIView背景色不生效

ios - 基本的 Objective C 语法

iphone - 如何在 iOS 中创建自定义键盘,如表情符号

ios - 模态视图 Controller 中的文本不会在 View Controller 中更改

swift - Bool 谓词导致零行计数

ios - 如何使用 Swift 为 WKWebView 安装插件?

iphone - 在 iOS 中一次性登录 - 最有效的方式?

ios - Segue 被调用了两次,我不确定为什么 - Swift

iphone - 如何在XCode(iOS SDK)中添加按钮到注释 View