Friday, 28 June 2013

Show picker view (UIDatePicker) on tapping text field.


We 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.

No comments:

Post a Comment