UIDatePicker崩溃的应用程序

时间:2016-07-28 11:40:25

标签: ios swift datepicker swift2

在我的应用中,我有一个textfield表单,我希望从日期选择器中填充,我已将日期选择器指定为文本字段的输入视图,并使用datePickerValueChanged func更改文本选择器更改时的文本字段。我的问题是,当点击应用程序中的数据选择器时,整个应用程序崩溃

  

'以NSException'

类型的未捕获异常终止

任何想法?这是我的代码:

@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)

}

这都包含在视图控制器" popoverTableViewController"的类中。谢谢!

以下是日志中的崩溃报告:

  

2016-07-28 12:50:00.956 ClockIn_v2 [5935:2049267]    - [ClockIn_v2.popoverTableViewController startDateDidBegin:]:无法识别的选择器发送到实例0x7ff571c10720 2016-07-28   12:50:00.961 ClockIn_v2 [5935:2049267] *终止应用程序   未捕获的异常' NSInvalidArgumentException',原因:   ' - [ClockIn_v2.popoverTableViewController startDateDidBegin:]:   无法识别的选择器发送到实例0x7ff571c10720'   * 第一次抛出调用堆栈:(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图形服务
  0x0000000114629a71 GSEventRunModal + 161 34 UIKit
  0x0000000110278e9c UIApplicationMain + 159 35 ClockIn_v2
  0x000000010eec96df main + 111 36 libdyld.dylib
  0x00000001136a968d start + 1)libc ++ abi.dylib:终止于   NSException(lldb)

类型的未捕获异常

1 个答案:

答案 0 :(得分:2)

而不是使用

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

在助理编辑器中打开它,在一个部分打开故事板,右键单击日期选择器,在事件列表中,单击“值已更改”(当您将鼠标悬停在其上时,它将显示一个小+''符号前面的空洞)并将其扩展到“popoverTableViewController.swift”文件。