如何将UIDatePicker的出生日期添加到UITextField?

时间:2015-05-04 16:22:29

标签: ios objective-c uitextfield date-of-birth

我在申请的注册屏幕上请求用户出生日期。我无法弄清楚如何显示出生日期UIDatePicker。我尝试使用以下代码,但它只显示日期和时间:

 UIDatePicker *dp = [[UIDatePicker alloc]init];
_textBirthday.inputView = dp;

另外,我如何设置它以便它只接受在某一年后出生的用户?

3 个答案:

答案 0 :(得分:17)

1)在视图控制器的.h文件中,确保指定textfield delegate:

@interface YourViewController : UIViewController <UITextFieldDelegate>

并且还有生日文本字段的IBOutlet

2)将日期选择器声明为类变量,以使其可以从类中的所有不同方法访问。在.m文件中,在导入之后和实现之前执行以下操作:

@interface YourViewController () {
UIDatePicker *datePicker;
}
@end

3)在viewdidload中:

// make the textfield its own delegate
self.BirthdateTextfield.delegate = self;

// alloc/init your date picker, and (optional) set its initial date
datePicker = [[UIDatePicker alloc]init];
[datePicker setDate:[NSDate date]]; //this returns today's date

// theMinimumDate (which signifies the oldest a person can be) and theMaximumDate (defines the youngest a person can be) are the dates you need to define according to your requirements, declare them:

// the date string for the minimum age required (change according to your needs) 
NSString *maxDateString = @"01-Jan-1996";
// the date formatter used to convert string to date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// the specific format to use
dateFormatter.dateFormat = @"dd-MMM-yyyy";
// converting string to date
NSDate *theMaximumDate = [dateFormatter dateFromString: maxDateString];

// repeat the same logic for theMinimumDate if needed

// here you can assign the max and min dates to your datePicker 
[datePicker setMaximumDate:theMaximumDate]; //the min age restriction 
[datePicker setMinimumDate:theMinimumDate]; //the max age restriction (if needed, or else dont use this line)

// set the mode
[datePicker setDatePickerMode:UIDatePickerModeDate];

// update the textfield with the date everytime it changes with selector defined below
[datePicker addTarget:self action:@selector(updateTextField:) forControlEvents:UIControlEventValueChanged];

// and finally set the datePicker as the input mode of your textfield
[self.BirthdateTextfield setInputView:datePicker];

4)在同一个.m文件中,定义每次日期选择器更改时都会更新文本字段的选择器:

-(void)updateTextField:(id)sender {
    UIDatePicker *picker = (UIDatePicker*)self.BirthdateTextfield.inputView;
    self.BirthdateTextfield.text = [self formatDate:picker.date];
}

5)最后但并非最不重要的是,这是在将日期分配给textfield.text之前调用的方法(textfield.text需要字符串而不是日期):

- (NSString *)formatDate:(NSDate *)date {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterShortStyle];
    [dateFormatter setDateFormat:@"dd-MMM-yyyy"];
    NSString *formattedDate = [dateFormatter stringFromDate:date];
    return formattedDate;
}

享受编码!

答案 1 :(得分:1)

使用以下代码,您将自动获得输出:

- (void)viewDidLoad {
[super viewDidLoad];

UIDatePicker *datePicker = [[UIDatePicker alloc]init];
[datePicker setDate:[NSDate date]];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:@selector(dateTextField:) forControlEvents:UIControlEventValueChanged];
[self.dobTextField setInputView:datePicker];
}

-(void) dateTextField:(id)sender{
 UIDatePicker *picker = (UIDatePicker*)self.dobTextField.inputView;
[picker setMaximumDate:[NSDate date]];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSDate *eventDate = picker.date;
[dateFormat setDateFormat:@"dd/MM/yyyy"];
NSString *dateString = [dateFormat stringFromDate:eventDate];
self.dobTextField.text = [NSString stringWithFormat:@"%@",dateString];
}

以下是Swift 4代码:

override func viewDidLoad() {
    super.viewDidLoad()
  showDatePicker()
}

//MARK: Show Date Picker
func showDatePicker(){
    //Formate Date
    datePicker.datePickerMode = .date
    //ToolBar
    let toolbar = UIToolbar();
    toolbar.sizeToFit()
    //done button & cancel button
    let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(donedatePicker))
    let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
    let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancelDatePicker))

    toolbar.setItems([cancelButton,spaceButton,doneButton], animated: false)

    // add toolbar to textField
    dateOfBirthTxtFldRef.inputAccessoryView = toolbar
    // add datepicker to textField
    dateOfBirthTxtFldRef.inputView = datePicker
}

@objc func donedatePicker(){
    //For date formate
    let formatter = DateFormatter()
    formatter.dateFormat = "dd MMMM yyyy"
    dateOfBirthTxtFldRef.text! = formatter.string(from: datePicker.date)
    //dismiss date picker dialog
    self.view.endEditing(true)
}

@objc func cancelDatePicker(){
    //cancel button dismiss datepicker dialog
    self.view.endEditing(true)
}

答案 2 :(得分:0)

datePickerMode设置为UIDatePickerModeDate

datePicker.datePickerMode = UIDatePickerModeDate;
  

Apple Docs:日期选择器显示月份,月份和年份。这些项目的确切顺序取决于区域设置。这种模式的一个例子是[11月| 15 | 2007]。   适用于iOS 2.0及更高版本。