user2662468 user2662468 - 4 months ago 25
iOS Question

UIDatePicker crashing application

In my app I have a

textfield
form that i want populated from a date picker, I have assigned the date picker as the input view for the textfield and a
datePickerValueChanged
func to change the text of the textfield when the picker is changed. My issue is that when clicking off the data picker in the app, the whole app crashes with


'terminating with uncaught exception of type NSException'


any ideas? Here is my code:

@IBOutlet var startDatePickerField: UITextField!

override func viewDidLoad() {

let startDatePicker:UIDatePicker = UIDatePicker()
startDatePicker.datePickerMode = UIDatePickerMode.dateAndTime
startDatePickerField.inputView = startDatePicker
startDatePicker.addTarget(self, action: #selector(popoverTableViewController.datePickerValueChanged(_:)), for: UIControlEvents.valueChanged)

}

func datePickerValueChanged(_ sender: UIDatePicker) {

let dateFormatter = DateFormatter()
dateFormatter.dateStyle = DateFormatter.Style.long
dateFormatter.timeStyle = DateFormatter.Style.short
startDatePickerField.text = dateFormatter.string(from: sender.date)

}


This is all contained in the class for the view controller "popoverTableViewController". Thanks!

Here is the crash report from log:


2016-07-28 12:50:00.956 ClockIn_v2[5935:2049267]
-[ClockIn_v2.popoverTableViewController startDateDidBegin:]: unrecognized selector sent to instance 0x7ff571c10720 2016-07-28
12:50:00.961 ClockIn_v2[5935:2049267] * Terminating app due to
uncaught exception 'NSInvalidArgumentException', reason:
'-[ClockIn_v2.popoverTableViewController startDateDidBegin:]:
unrecognized selector sent to instance 0x7ff571c10720'
*
First throw call stack: ( 0 CoreFoundation 0x000000010fe5986b exceptionPreprocess + 171 1 libobjc.A.dylib

0x000000010f4b124e objc_exception_throw + 48 2 CoreFoundation

0x000000010fec7904 -[NSObject(NSObject) doesNotRecognizeSelector:] +
132 3 CoreFoundation 0x000000010fddfed5
___forwarding_
+ 1013 4 CoreFoundation 0x000000010fddfa58 _CF_forwarding_prep_0 + 120 5 UIKit

0x000000011027aaf0 -[UIApplication sendAction:to:from:forEvent:] + 83
6 UIKit 0x00000001103fda69
-[UIControl sendAction:to:forEvent:] + 67 7 UIKit 0x00000001103fdd82 -[UIControl _sendActionsForEvents:withEvent:] + 444
8 UIKit 0x0000000110d5b196
-[UITextField _resignFirstResponder] + 297 9 UIKit 0x000000011048d780 -[UIResponder _finishResignFirstResponder] + 286
10 UIKit 0x0000000110d5af94
-[UITextField _finishResignFirstResponder] + 49 11 UIKit 0x000000011048d82f -[UIResponder resignFirstResponder] + 140 12
UIKit 0x0000000110d5ae63 -[UITextField
resignFirstResponder] + 136 13 UIKit

0x000000011048d4bf -[UIResponder becomeFirstResponder] + 358 14
UIKit 0x0000000110328dae
-[UIView(Hierarchy) becomeFirstResponder] + 138 15 UIKit 0x0000000110d59d2e -[UITextField becomeFirstResponder] + 51 16 UIKit
0x00000001107af37b
-[UITextInteractionAssistant(UITextInteractionAssistant_Internal) setFirstResponderIfNecessary] + 206 17 UIKit

0x00000001107b2b46
-[UITextInteractionAssistant(UITextInteractionAssistant_Internal) oneFingerTap:] + 3823 18 UIKit

0x00000001107a081d -[UIGestureRecognizerTarget
_sendActionWithGestureRecognizer:] + 57 19 UIKit 0x00000001107a85b4 _UIGestureRecognizerSendTargetActions + 109 20
UIKit 0x00000001107a613b
_UIGestureRecognizerSendActions + 540 21 UIKit 0x00000001107a539d -[UIGestureRecognizer
_updateGestureWithEvent:buttonEvent:] + 1177 22 UIKit 0x00000001107911f2 _UIGestureEnvironmentUpdate + 1013 23 UIKit

0x0000000110790db5 -[UIGestureEnvironment
_deliverEvent:toGestureRecognizers:usingBlock:] + 521 24 UIKit 0x000000011078ff2c -[UIGestureEnvironment
_updateGesturesForEvent:window:] + 286 25 UIKit 0x00000001102e945c -[UIWindow sendEvent:] + 3989 26 UIKit

0x00000001102967a5 -[UIApplication sendEvent:] + 281 27 UIKit

0x0000000110a632c3 dispatchPreprocessedEventFromEventQueue + 3303
28 UIKit 0x0000000110a5be75
__handleEventQueue + 4879 29 CoreFoundation 0x000000010fdff5d1
__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION
+ 17 30 CoreFoundation 0x000000010fde485c
__CFRunLoopDoSources0 + 556 31 CoreFoundation 0x000000010fde3d46 __CFRunLoopRun + 918 32 CoreFoundation

0x000000010fde3754 CFRunLoopRunSpecific + 420 33 GraphicsServices

0x0000000114629a71 GSEventRunModal + 161 34 UIKit

0x0000000110278e9c UIApplicationMain + 159 35 ClockIn_v2

0x000000010eec96df main + 111 36 libdyld.dylib

0x00000001136a968d start + 1 ) libc++abi.dylib: terminating with
uncaught exception of type NSException (lldb)

Answer

instead of adding action using

startDatePicker.addTarget(self, action:  #selector(popoverTableViewController.datePickerValueChanged(_:)), for: UIControlEvents.valueChanged)  

Open it in Assistant editor, open storyboard in one part, right click on your date picker, in the list of events, click on "Value Changed" (it will show a little '+' sign when you will hover upon the empty hole in front of it) and extend it to your "popoverTableViewController.swift" file.