我正在尝试在我的UITextField中添加日期格式,我有一些教程,但我认为使用新版本的swift我有一些错误。
我正在尝试在UITextField上显示UIDatePicker:
@IBAction func tfDateNaissanceEditing(sender: UITextField) {
let datePickerView:UIDatePicker = UIDatePicker()
datePickerView.datePickerMode = UIDatePickerMode.Date
sender.inputView = datePickerView
datePickerView.addTarget(self, action: Selector("datePickerValueChanged:"), forControlEvents: UIControlEvents.ValueChanged)
}
@IBAction func tfDateNaissanceChanged(sender: UITextField) {
let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
dateFormatter.timeStyle = NSDateFormatterStyle.NoStyle
tfDateNaissance.text = dateFormatter.stringFromDate(sender.date)
}
但是我在sender.date上收到错误:UITextField没有成员日期。
我想知道,如果我能得到日期,我会如何检查用户是否超过16岁?
错误
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2015-12-22 16:12:51.699 Solutis[1315:72750] -[Solutis.DemandeGratuiteViewController datePickerValueChanged:]: unrecognized selector sent to instance 0x7aa7e000
2015-12-22 16:12:51.707 Solutis[1315:72750] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Solutis.DemandeGratuiteViewController datePickerValueChanged:]: unrecognized selector sent to instance 0x7aa7e000'
*** First throw call stack:
(
0 CoreFoundation 0x002e5a84 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x02185e02 objc_exception_throw + 50
2 CoreFoundation 0x002eedd3 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x0022ccdd ___forwarding___ + 1037
4 CoreFoundation 0x0022c8ae _CF_forwarding_prep_0 + 14
5 libobjc.A.dylib 0x0219a0b5 -[NSObject performSelector:withObject:withObject:] + 84
6 UIKit 0x00cd016a -[UIApplication sendAction:to:from:forEvent:] + 118
7 UIKit 0x00cd00e9 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 64
8 UIKit 0x00e6e19f -[UIControl sendAction:to:forEvent:] + 79
9 UIKit 0x00e6e51f -[UIControl _sendActionsForEvents:withEvent:] + 408
10 UIKit 0x00e6e1df -[UIControl sendActionsForControlEvents:] + 48
11 UIKit 0x0166fab1 -[_UIDatePickerView pickerView:didSelectRow:inComponent:] + 607
12 UIKit 0x00caecd7 -[UIPickerView _sendSelectionChangedForComponent:notify:] + 124
13 UIKit 0x00caee68 -[UIPickerView _sendSelectionChangedFromTable:notify:] + 137
14 UIKit 0x014ba31f -[UIPickerTableView _scrollingFinished] + 218
15 UIKit 0x014ba3d5 -[UIPickerTableView scrollViewDidEndDecelerating:] + 33
16 UIKit 0x00dc55a2 -[UIScrollView(UIScrollViewInternal) _stopScrollDecelerationNotify:] + 1306
17 UIKit 0x00dc582c -[UIScrollView(UIScrollViewInternal) _stopScrollingNotify:pin:tramplingDragFlags:] + 523
18 UIKit 0x00dc587e -[UIScrollView(UIScrollViewInternal) _stopScrollingNotify:pin:] + 57
19 UIKit 0x00dbaf7d -[UIScrollView _smoothScrollWithUpdateTime:] + 230
20 UIKit 0x00dbc41b -[UIScrollView _smoothScrollDisplayLink:] + 289
21 QuartzCore 0x05be0cfa _ZN2CA7Display15DisplayLinkItem8dispatchEv + 62
22 QuartzCore 0x05be0ba3 _ZN2CA7Display11DisplayLink14dispatch_itemsEyyy + 421
23 QuartzCore 0x05be10b5 _ZN2CA7Display16TimerDisplayLink8callbackEP16__CFRunLoopTimerPv + 123
24 CoreFoundation 0x00237576 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 22
25 CoreFoundation 0x00236f72 __CFRunLoopDoTimer + 1250
26 CoreFoundation 0x001f525a __CFRunLoopRun + 2202
27 CoreFoundation 0x001f4706 CFRunLoopRunSpecific + 470
28 CoreFoundation 0x001f451b CFRunLoopRunInMode + 123
29 GraphicsServices 0x05656664 GSEventRunModal + 192
30 GraphicsServices 0x056564a1 GSEventRun + 104
31 UIKit 0x00cce1eb UIApplicationMain + 160
32 Solutis 0x000652fc main + 140
33 libdyld.dylib 0x02be9a21 start + 1
34 ??? 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
超过18岁:
func verifAge(){
let start = String(tfDateNaissance.text!)
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy"
let startDate:NSDate = dateFormatter.dateFromString(start)
let cal = NSCalendar.currentCalendar()
let components = cal.components(.Year, fromDate: startDate, toDate: NSDate(), options: [])
print(components)
}
致命错误:在解包可选值时意外发现nil
答案 0 :(得分:0)
首先,要从DatePicker
获取所需日期,sender
是一种UIDatePicker
,而不是您方法输入中声明的UITextField
。
其次,请参阅this answer,您可以通过
获取年龄let ageComponents = calendar.components(.Year, fromDate: sender.date, toDate: NSDate(), options: [])
修改强>
您添加了一个名为datePickerValueChanged
的选择器方法,但该操作名为tfDateNaissanceChanged
。或者我应该这样说,你不需要听TextView
值改变,只需要实现DataPicker
值改变的方法。