开发者

datepicker by clicking on textfield [duplicate]

开发者 https://www.devze.com 2023-04-03 06:03 出处:网络
This question already has answers here: Display datepicker on tapping on textfield 开发者_JAVA百科
This question already has answers here: Display datepicker on tapping on textfield 开发者_JAVA百科 (5 answers) Closed 9 years ago.

I am trying to implement the the date picker by clicking on textfield. I am using the code which is mentioned in the this link.

But I am not getting the output. If someone know how to bring the date picker by clicking on textfield. Please provide me some solution for it.

Thanks in advance.


I see your link and got the solution so try the below code.

#import "TextfieldwithDatepickerViewController.h"

UIActionSheet *pickerViewPopup;

@implementation TextfieldwithDatepickerViewController
- (void)textFieldDidBeginEditing:(UITextField *)aTextField{  
    [aTextField resignFirstResponder];  

    pickerViewPopup = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];  

    UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];  
    pickerView.datePickerMode = UIDatePickerModeDate;  
    pickerView.hidden = NO;  
    pickerView.date = [NSDate date];  

    UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];  
    pickerToolbar.barStyle = UIBarStyleBlackOpaque;  
    [pickerToolbar sizeToFit];  

    NSMutableArray *barItems = [[NSMutableArray alloc] init];  

    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];  
    [barItems addObject:flexSpace];  

    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)];  
    [barItems addObject:doneBtn];  

    UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed:)];  
    [barItems addObject:cancelBtn];  

    [pickerToolbar setItems:barItems animated:YES];  

    [pickerViewPopup addSubview:pickerToolbar];  
    [pickerViewPopup addSubview:pickerView];  
    [pickerViewPopup showInView:self.view];  
    [pickerViewPopup setBounds:CGRectMake(0,0,320, 464)];  
}  

-(void)doneButtonPressed:(id)sender{  
    //Do something here here with the value selected using [pickerView date] to get that value  

    [pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES];  
}  

-(void)cancelButtonPressed:(id)sender{  
    [pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES];  
}  

This code is definitely help you because I run the code and its work fine.


-(BOOL) textFieldShouldBeginEditing:(UITextField *)txtField
{
[self createDatePicker];
}

set Delegate TextFeild also


you can put your date picker in another view in interface builder ... and put the view under the main view (x = 0; y = 460).

So when your textfield becomes first responder (there are some delegate methods for this like – textFieldDidBeginEditing:) ... just animate the view with the datePicker in the desired position.

Animation can be done like ...

[UIView beginAnimations:@"some_random_name" context:nil];

    //give your date picker view a new frame

[UIView setAnimationDuration:0.3f]; //or any other value
[UIView commitAnimations];


Check that you set the delegate for the textfield. If not, set like this:

textField.delegate = self;

Or in xib add connection to the delegate.


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.


Use the following code:

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[actionSheet showInView:self.tabBarController.tabBar];
[actionSheet setFrame:CGRectMake(0, 220, 320,350)];
pickerToolbar.barStyle = UIBarStyleDefault;
[pickerToolbar sizeToFit];
pickerToolbar.barStyle = UIBarStyleBlackTranslucent;

NSMutableArray *barItems = [[NSMutableArray alloc] init];

UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:[BuddyCalcAppDelegate getLocalvalue:@"Cancel"] style:UIBarButtonSystemItemCancel target:self action:@selector(cancel_clicked:)];
[barItems addObject:cancelBtn];
[cancelBtn release];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[barItems addObject:flexSpace];
[flexSpace release];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:[BuddyCalcAppDelegate getLocalvalue:@"Done"] style:UIBarButtonItemStyleDone target:self action:@selector(done_clicked:)];
[barItems addObject:doneBtn];
[doneBtn release];
[pickerToolbar setItems:barItems animated:YES];
[actionSheet addSubview:pickerToolbar];
[barItems release];
[pickerToolbar release];
UIDatePicker *datePicker=[[UIDatePicker alloc]init];
if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight) 
{
    datePicker.frame=CGRectMake(0,32,480, 216); 
    [scrollView setContentOffset:CGPointMake(0, 60) animated:YES];
} else {
    datePicker.frame=CGRectMake(0,44,320, 216);
}

datePicker.datePickerMode = UIDatePickerModeDateAndTime;
[datePicker setMinuteInterval:5];
[datePicker setTag:10];
[actionSheet addSubview:datePicker];
[datePicker addTarget:self action:@selector(Result) forControlEvents:UIControlEventValueChanged];
[datePicker release];
0

精彩评论

暂无评论...
验证码 换一张
取 消