I want to use sender to get the value from segmented control, but I am getting the unrecognized selector sent to instance error. The segmented control was added within a view. I've tried removing the semicolon and/or sender and none of the prior posts I've found seem to help. Any ideas?
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)
Error code:
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)