Ok so I’ve gone through pretty much all the SO articles on this subject and can’t resolve my particular situation. I have a view controller with 3 text fields and a text area. One of the text fields is for a date. When the user enters this field, I want to hide the keyboard and show the date picker.
The issue I run into with my code so far is that the keyboard shows up when I enter any of the fields, but then does not disappear no matter what I’ve tried.
I could not find any sample app that has this feature and would like some direction on how to resolve this issue.
UPDATE: I fixed the first part of this and now can hide my keyboard when I get to the date field.

This was using the below code:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if ( textField.tag == 1 )
{
[self.view endEditing:TRUE];
[lblDesc2 resignFirstResponder];
[lblTitle resignFirstResponder];
[lblExcerpt resignFirstResponder];
NSLog(@"The method was called for textField ");
[self showDatePicker];
return NO;
}
else
{
return YES;
}
}
Now what I need to do is pull up the date picker and here is my code for that which is not working yet:
- (void) showDatePicker{
CGRect pickerFrame = CGRectMake(0,250,325,0);
datePicker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
[datePicker addTarget:self action:@selector(pickerChanged:) forControlEvents:UIControlEventValueChanged];
[scrollView addSubview:datePicker];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker release];
}
.h
#import <UIKit/UIKit.h>
#import "QuoteViewController.h"
#import "SubjectViewController.h"
#import "CategoryViewController.h"
@class Quote, SubjectViewController, QuoteViewController;
@interface AddQuoteViewController : UIViewController <UITextFieldDelegate>{
@private
UIDatePicker *datePicker;
UIBarButtonItem *doneButton; // this button appears only when the date picker is open
NSArray *dataArray;
NSDateFormatter *dateFormatter;
IBOutlet UITextField *lblTitle;
IBOutlet UITextField *lblDesc2;
IBOutlet UITextField *lblDate;
IBOutlet UITextView *lblExcerpt;
IBOutlet UITextField *lblNote;
QuoteViewController *qvc;
SubjectViewController *svc;
//UITextField *editingField;
IBOutlet UIScrollView *scrollView;
}
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) IBOutlet UIDatePicker *datePicker;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *doneButton;
@property (nonatomic, retain) NSArray *dataArray;
@property (nonatomic, retain) NSDateFormatter *dateFormatter;
//- (IBAction)doneAction:(id)sender; // when the done button is clicked
//- (IBAction)dateAction:(id)sender; // when the user has changed the date picke values (m/d/y)
@property (nonatomic,assign) QuoteViewController *qvc;
@property (nonatomic,assign) SubjectViewController *svc;
@end
.m
#import "AddQuoteViewController.h"
#import "Category.h"
#import "QuotesAppDelegate.h"
#import "Quote.h"
#import "QuoteMap.h"
@implementation AddQuoteViewController
@synthesize svc, qvc;
@synthesize datePicker, doneButton, dataArray, dateFormatter, scrollView;
// Implement viewDidLoad to do additional setup after loading the view.
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Add Quote";
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self action:@selector(cancel_Clicked:)] autorelease];
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemSave
target:self action:@selector(save_Clicked:)] autorelease];
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
self.dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[self.dateFormatter setDateStyle:NSDateFormatterShortStyle];
[self.dateFormatter setTimeStyle:NSDateFormatterNoStyle];
CGRect pickerFrame = CGRectMake(0,250,325,0);
datePicker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
[datePicker addTarget:self action:@selector(pickerChanged:) forControlEvents:UIControlEventValueChanged];
[scrollView addSubview:datePicker];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
datePicker.datePickerMode = UIDatePickerModeDate;
lblDate.tag = 1;
[datePicker release];
}
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
//Set the textboxes to empty string.
//Display the selected quote.
lblExcerpt.text = @"";
lblTitle.text = @"";
lblDesc2.text = @"";
lblDate.text = @"";
NSLog(@"AddQuoteViewController initialized...");
//Make the Category name textfield to be the first responder.
// [lblTitle becomeFirstResponder];
}
- (void)pickerChanged:(id)sender
{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
lblDate.text = [NSString stringWithFormat:@"%@",
[df stringFromDate:datePicker.date]];
NSLog(@"value: %@",[sender date]);
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if ( textField.tag == 1 )
{
[self.view endEditing:TRUE];
[lblDesc2 resignFirstResponder];
[lblTitle resignFirstResponder];
[lblExcerpt resignFirstResponder];
NSLog(@"The method was called for textField ");
[self showDatePicker];
return NO;
}
else
{
return YES;
}
}
- (void)viewDidUnload
{
self.dataArray = nil;
self.dateFormatter = nil;
}
- (void)dealloc {
[lblTitle release];
[lblDesc2 release];
[lblDate release];
[lblExcerpt release];
[doneButton release];
[dataArray release];
[scrollView release];
[datePicker release];
[dateFormatter release];
[super dealloc];
}
@end
1 Answer