ios - 分段控制 - 发送到实例的无法识别的选择器

标签 ios swift uiview uiviewcontroller uisegmentedcontrol

我想使用 sender 从分段控件中获取值,但我收到了无法识别的选择器发送到实例错误。分段控件已添加到 View 中。我试过删除分号和/或发件人,但我发现之前的帖子似乎都没有帮助。有什么想法吗?

快速代码

        var segCntrl = UISegmentedControl(items: ["Yes","No"])
        segCntrl.tag = 100
        segCntrl.center = CGPointMake(qView.bounds.width/2, qView.bounds.minY+40)
        self.qView.addSubview(segCntrl)

        func segmentorSwitch(sender: AnyObject) {
            if(sender.selectedSegmentIndex == 1) {
                println("Yes selected")
            }
            else if(sender.selectedSegmentIndex == 2) {
                println("No selected")
            }
        }
        segCntrl.addTarget(self, action: "segmentorSwitch:", forControlEvents: UIControlEvents.ValueChanged)

错误代码:

2015-07-07 11:46:24.482 myApp [75641:1855377] -[[myApp.SegViewControllerr segmentorSwitch:]: unrecognized selector sent to instance 0x7fbb0a43b450 2015-07-07 11:46:24.489 myApp[75641:1855377] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[myApp.SegViewController segmentorSwitch:]: unrecognized selector sent to instance 0x7fbb0a43b450' * First throw call stack: ( 0 CoreFoundation 0x00000001060dcc65 exceptionPreprocess + 165 1 libobjc.A.dylib 0x000000010838abb7 objc_exception_throw + 45 2 CoreFoundation 0x00000001060e40ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205 3 CoreFoundation 0x000000010603a13c ___forwarding_ + 988 4 CoreFoundation 0x0000000106039cd8 _CF_forwarding_prep_0 + 120 5 UIKit 0x00000001070bfd62 -[UIApplication sendAction:to:from:forEvent:] + 75 6 UIKit 0x00000001071d150a -[UIControl _sendActionsForEvents:withEvent:] + 467 7 UIKit 0x000000010724dfba -[UISegmentedControl _setSelectedSegmentIndex:notify:animate:] + 570 8 UIKit 0x000000010724ffbf -[UISegmentedControl touchesEnded:withEvent:] + 143 9 UIKit 0x000000010710c958 -[UIWindow _sendTouchesForEvent:] + 735 10 UIKit 0x000000010710d282 -[UIWindow sendEvent:] + 682 11 UIKit 0x00000001070d3541 -[UIApplication sendEvent:] + 246 12 UIKit 0x00000001070e0cdc _UIApplicationHandleEventFromQueueEvent + 18265 13 UIKit 0x00000001070bb59c _UIApplicationHandleEventQueue + 2066 14 CoreFoundation 0x0000000106010431 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 15 CoreFoundation 0x00000001060062fd __CFRunLoopDoSources0 + 269 16 CoreFoundation 0x0000000106005934 __CFRunLoopRun + 868 17 CoreFoundation 0x0000000106005366 CFRunLoopRunSpecific + 470 18 GraphicsServices 0x000000010cc5ba3e GSEventRunModal + 161 19 UIKit 0x00000001070be8c0 UIApplicationMain + 1282 20 myApp 0x0000000104d9a0c7 main + 135 21 libdyld.dylib 0x0000000109458145 start + 1 22 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

最佳答案

看起来 segmentorSwitch: 是在另一个方法中实现的。在包含您的代码的方法之外定义它,这应该是固定的。

关于ios - 分段控制 - 发送到实例的无法识别的选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31273786/

相关文章:

ios - UITabBarController 上的项目显示不正确

ios - label.isHidden = false 不出现,Swift

ios - 带定时器的 UIButton

ios - iPhone日期格式中译英

ios - Swift - 无法读取数据,因为它的格式不正确

ios - 查看自定义设计

ios - 在 iPad 上使用 UIActionSheet 时调暗并锁定背景

objective-c - X代码 4.2 : Tracking Down Deallocated NSString

ios - 刷新配置文件时出现 XCode "Too few items in teams"错误

ios - 应用内购买/快速转换