ductran
ductran

Reputation: 10203

Select date in UIDatePicker from UITextField click event MonoTouch

First, I have an TextField. When user click on it, DatePicker will appear (in the same textfield's view). After user choose date, DatePicker should be disappear, and textfield should show selected date.
I have tried this code in ViewDidLoad:

    txtTime.TouchDown += (sender, e) => {
    UIDatePicker picker = new UIDatePicker (new RectangleF (0, 20, this.View.Bounds.Width, 10));  
    picker.Date = DateTime.Now;
    picker.Mode = UIDatePickerMode.Time;
    this.Add(picker);
    txtTime.ResignFirstResponder();             
};

But I have some problem:

  1. Can't disable keyboard (ResignFirstResponder() doesn't work)
  2. I don't know how to handle select date even of DatePicker to set for Textbox
  3. The DatePicker appear without 'close' or 'done' button, so can't return to textfield view (could I use Datepicker without actionsheet?).

How can I solve that?

Upvotes: 1

Views: 2296

Answers (2)

user2731717
user2731717

Reputation: 75

this.txtTime.shouldreturn += (txtTime) => {
txtTime.resignfirstresponder ();
return true;
}

Upvotes: 0

Maxim Korobov
Maxim Korobov

Reputation: 2570

In the book "Developing CSharp Apps for iPhone and iPad using MonoTouch - 2011", chapter 7 "Standard Controls" you could see example of how to implement picking date.

Basic idea is to:

  • Put DatePicker on a UIActionSheet (create custom class, which controls DatePicker and UIActionSheet actually);
  • Add label "Choose date, then press OK" and buttons "OK"/"Cancel" to the top side of UIActionSheet's View;
  • Show UIActionSheet on textField.EditingDidBegin event.

Screenshot:

How it looks like

Sorry for Russian locale settigs.

Upvotes: 1

Related Questions