选择DatePicker时隐藏键盘 - 问题未解决

时间:2012-02-27 02:19:20

标签: iphone objective-c keyboard datepicker

好的,所以我已经完成了关于这个主题的所有SO文章,并且无法解决我的特殊情况。我有一个视图控制器有3个文本字段和一个文本区域。其中一个文本字段是日期。当用户输入此字段时,我想隐藏键盘并显示日期选择器。

到目前为止,我遇到的问题是当我输入任何字段时键盘出现,但无论我尝试过什么,它都不会消失。

我找不到任何具有此功能的示例应用,并希望了解如何解决此问题。

更新:我修复了第一部分,现在可以在到达日期字段时隐藏键盘。

Here is what it looks like when I click on the date field

这是使用以下代码:

- (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) 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 个答案:

答案 0 :(得分:0)

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

 //do what you want to do when user is touching the textfield. e.g. Display the DatePicker 

       return NO;

}