shreedevi
shreedevi

Reputation: 539

hide the keypad of the textfield and display datepicker

i have a form wher i need to enter the start date and end date to view the transactions. when i set the focus on the textfirld, i can see the keypad, but behind it the datepicker is coming. how am i remove the keypad and only show the datepicker

Upvotes: 2

Views: 10207

Answers (6)

HDdeveloper
HDdeveloper

Reputation: 4404

Now you can use inputView and inputAccessoryView of UITextField. Declare in .h file, and @synthesize in .m file.

@property (nonatomic, retain) IBOutlet UIToolbar    *_accessoryView;
@property (nonatomic, retain) IBOutlet UIDatePicker *_datePickerView;

Connect them to File's Owner in .xib file, make sure that its out of the self.view.

- (void)viewDidLoad
    {   
        _dateOfBirthTextField.inputView = self._datePickerView;
        _dateOfBirthTextField.inputAccessoryView = self._toolbar;
    }

Also write function to fetch date from the picker

#pragma mark- UITextField with UIDatePicker-

- (IBAction)dateChanged:(id)sender {
    UIDatePicker *picker = (UIDatePicker *)sender;
    _dateOfBirthTextField.text = [NSString stringWithFormat:@"%@", picker.date];
}

- (IBAction)doneEditing:(id)sender {
    [_dateOfBirthTextField resignFirstResponder];
}

Add dateChanged: IBAction on valueChanged: action of _datePicker. and doneEditing: IBAction on toolbar barbutton action.

Upvotes: 0

oscar castellon
oscar castellon

Reputation: 3138

Very simple, in viewController.h put the UITextFieldDelegate. After in viewDidLoad.

 txtFecha.delegate = self;

datePicker = [[UIDatePicker alloc]init];
[datePicker setDatePickerMode:UIDatePickerModeDate];

self.txtFecha.inputView = datePicker;

Upvotes: 0

Chris
Chris

Reputation: 107

I struggled with that myself and found the solution. You can do it this way:

- (void)viewDidLoad 
{
    [super viewDidLoad];
// set delegate for date text field
    dateField.delegate = self;
}

// Display the picker instead of a keyboard
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    [self showDatePicker];
    return NO;

}

This assumes, that you have declared a method called showDatePicker, which handles the UIPicker display.

Upvotes: 3

wkw
wkw

Reputation: 3863

It sounds like instead of a standard keypad, you instead want to show your own datepicker view. I believe you can handle that by setting the delegate for your textfield then providing textFieldShouldBeginEditing.

// in viewWillAppear set the textfield delegate if needed

   ...
   myTextField.delegate = self;
   ...

    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{

        // if date picker not in display, show it here...

        return NO;
    }

Upvotes: 2

Morion
Morion

Reputation: 10860

you can call [myTextField resignFirsrResponder] on some button click method or, for example, in textFieldShouldReturn delegate method. smth like that

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

Upvotes: 1

Related Questions