分段控制 - 发送到实例

时间:2015-07-07 16:03:18

标签: ios swift uiview uiviewcontroller uisegmentedcontrol

我想使用sender从分段控件中获取值,但是我将无法识别的选择器发送到实例错误。在视图中添加了分段控件。我已经尝试删除分号和/或发件人,我发现以前的帖子似乎没有任何帮助。有什么想法吗?

Swift Code

        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:]:无法识别的选择器发送到实例0x7fbb0a43b450   2015-07-07 11:46:24.489 myApp [75641:1855377] *由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [myApp.SegViewController segmentorSwitch:]:无法识别的选择器发送到实例0x7fbb0a43b450'   * 第一次抛出调用堆栈:   (       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:以NSException类型的未捕获异常终止   (lldb)

1 个答案:

答案 0 :(得分:2)

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