用TableViewController中的日期选择器替换iOS7键盘

时间:2014-04-22 01:26:35

标签: ios uidatepicker

我想要我的textfield" DateText"使用此类值打开DatePicker(年 - 月 - 日):

YYYY-MM-DD

有没有简单的方法来更改键盘?

3 个答案:

答案 0 :(得分:33)

您可以使用UITextField的inputView属性“替换”键盘:

- (void) initializeTextFieldInputView {
    UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectZero];
    datePicker.datePickerMode = UIDatePickerModeDate;
    [datePicker addTarget:self
                   action:@selector(dateUpdated:)
         forControlEvents:UIControlEventValueChanged];
    self.textField.inputView = datePicker;
}

- (void) dateUpdated:(UIDatePicker *)datePicker {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    self.textField.text = [formatter stringFromDate:datePicker.date];
}

UIDatePickers没有像UIPickerViews这样的委托,所以这就是为什么必须添加一个动作,就像它是一个UIButton一样。选择器声明末尾的:提供了UIDatePicker,以方便获取更改日期。如果您不想在方法中发送对象,可以通过引用UITextField的inputView来获取UIDatePicker:

UIDatePicker *datePicker = (UIDatePicker *)self.textField.inputView;

最后,在使用NSDateFormatter设置格式时,最好创建一个方法来返回NSDateFormatter对象,以确保格式一致。至少,在项目中创建一个常量/宏,声明格式字符串。

修改

正如Pavan在评论主题中所说,如果你想在键盘上方添加一个完成按钮,请使用inputAccessoryView:

UIToolbar *toolbar = [[UIToolbar alloc] init];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneButtonWasPressed:)];
UIBarButtonItem *flexibleSeparator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
toolbar.items = @[flexibleSeparator, doneButton];
self.textField.inputAccessoryView = toolbar;

编辑2,SWIFT

看起来像这样答案已经过时了。使用Swift 3添加实现:

@IBOutlet weak var textField: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()
    self.initializeTextFieldInputView()
}

func initializeTextFieldInputView() {
    // Add date picker
    let datePicker = UIDatePicker()
    datePicker.datePickerMode = .date
    datePicker.addTarget(self, action: #selector(dateChanged(_:)), for: .valueChanged)
    self.textField.inputView = datePicker

    // Add toolbar with done button on the right
    let toolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 1))
    let flexibleSeparator = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
    let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(doneButtonPressed(_:)))
    toolbar.items = [flexibleSeparator, doneButton]
    self.textField.inputAccessoryView = toolbar
}

func dateChanged(_ datePicker: UIDatePicker) {
    let formatter = DateFormatter()
    formatter.dateFormat = "yyyy-MM-dd"
    self.textField.text = formatter.string(from: datePicker.date)
}

您还可以通过在UITextField中投射inputView来获取UIDatePicker:

let datePicker = self.textField.inputView as? UIDatePicker

答案 1 :(得分:0)

Swift 2.0

我发现克里斯的答案很有帮助,但在Swift中需要它。

override func viewDidLoad() {
    super.viewDidLoad()

    // Present UIDatePicker for date input
    let datePicker = UIDatePicker(frame: CGRectZero)
    datePicker.datePickerMode = .Date
    datePicker.addTarget(self, action: #selector(SettingsViewController.DateChanged), forControlEvents: .ValueChanged)
    self.fromTimeField.inputView = datePicker
}

 func timeChanged(datePicker: UIDatePicker) {
    let formatter = NSDateFormatter()
    formatter.dateFormat = "yyyy-MM-dd"
    self.fromTimeField.text = formatter.stringFromDate(datePicker.date)

答案 2 :(得分:-1)

在创建TextField操作时使用“编辑已开始”操作启用UIDatePicker。

- (IBAction)textFieldAction:(id)sender
{
    // Place your code for UIDatePicker
    [textField resignFirstResponder]; //add this line in the end.
}