【问题标题】:Birthday date in UITextField and check ageUITextField 中的生日日期并检查年龄
【发布时间】:2015-12-22 14:37:45
【问题描述】:

我正在尝试在我的 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 has no member date.

我想知道,如果我能得到日期,我将如何检查用户是否超过 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

【问题讨论】:

  • 在您的第二个操作中,您尝试对UITextField 类型的实例使用方法.date,而后一种类型不包含名为.date 的方法。 (tfDateNaissance.text = dateFormatter.stringFromDate(**sender.date**))。也许您打算在这里改用sender.text

标签: ios swift uitextfield uidatepicker


【解决方案1】:

首先,要从DatePicker 中获取您想要的日期,senderUIDatePicker 的类型,而不是您的方法输入中声明的UITextField

其次,参考this answer,可以得到年龄

let ageComponents = calendar.components(.Year, fromDate: sender.date, toDate: NSDate(), options: [])

编辑

您添加了一个名为 datePickerValueChanged 的选择器方法,但该操作名为 tfDateNaissanceChanged。或者我应该这样说,你不需要听TextView值改变,只需要实现DataPicker值改变的方法。

【讨论】:

  • 我将发件人更改为 UIDatePicker,但在选择日期时出现错误,请查看我的编辑。
  • @Ben 您需要在视图控制器类中声明 let datePickerView = UIDatePicker() (将其移出 IBAction 方法)并使用 datePickerView.date
  • @LeoDabus 嗨,这个 Picker 视图的格式不好,是时候了
  • @Ben,确保你的函数名为datePickerValueChanged
  • @Ben 没有日期就没有时间
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-04
  • 1970-01-01
  • 1970-01-01
  • 2021-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多