ios - UIDatePicker 崩溃应用程序

标签 ios swift datepicker swift2

在我的应用程序中,我有一个要从日期选择器填充的 textfield 表单,我已将日期选择器指定为文本字段的输入 View 和一个 datePickerValueChanged更改选择器时更改文本字段文本的功能。我的问题是,当点击应用程序中的数据选择器时,整个应用程序崩溃并显示

'terminating with uncaught exception of type 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)

}

这都包含在 View Controller “popoverTableViewController”的类中。谢谢!

这是来自日志的崩溃报告:

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)

最佳答案

而不是使用

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

在 Assistant editor 中打开它,在一个部分中打开 Storyboard,右键单击您的日期选择器,在事件列表中,单击“值已更改”(当您将鼠标悬停在它前面的空洞)并将其扩展到您的“popoverTableViewController.swift”文件。

关于ios - UIDatePicker 崩溃应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38635311/

相关文章:

ios - 保存一个带有另一个蒙版 ImageView 的 Image View 吗?

swift - 为什么 UIViews 背景颜色没有更新?

jquery - jquery日期选择器是否允许您有一个日期和时间选择器

ios - 在 Swift 中带有标识符的 CustomModalViewController

ios - Swift - 从 peoplePickerNavigationController 访问名称

javascript - Bootstrap-Datepicker 图标点击事件不起作用

javascript - 选择月份后,jQuery UI 月份选择器应关闭

ios - 我们可以在 iOS 应用程序中按需添加 Twitter 权限吗?

iOS 在 UIWebview 中加载和查看 .docx 文件

ios - 在 iPhone 模拟器中,如何在带有德语键盘的 macbook 上输入 @ 符号?