how to show datetimepicker and set the chosen value in textfield

Mishal Awan

hi i am new to iphone programming .This program i will be doing for iphone so i want help. My question is by clicking a button can date picker appear and the when the date is chosen , i want it to be stored in a text field.How it can be done . kindly help me.I shall be very thankful to you. i had a button on which i want to show a date time picker how it can be done ? below is my try code but it is not working just showing black screen.

- (IBAction)date:(id)sender {
    UIDatePicker* picker = [[UIDatePicker alloc] init];
    picker.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    picker.datePickerMode = UIDatePickerModeDate;

    [picker addTarget:self action:@selector(dueDateChanged:) forControlEvents:UIControlEventValueChanged];
    CGSize pickerSize = [picker sizeThatFits:CGSizeZero];
    picker.frame = CGRectMake(0.0, 250, pickerSize.width, 460);
    picker.backgroundColor = [UIColor blackColor];
    [self.view addSubview:picker];
   // [picker release];

}
-(void) dueDateChanged:(UIDatePicker *)sender {
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterLongStyle];
    [dateFormatter setTimeStyle:NSDateFormatterNoStyle];

    //self.myLabel.text = [dateFormatter stringFromDate:[dueDatePickerView date]];
    NSLog(@"Picked the date %@", [dateFormatter stringFromDate:[sender date]]);
}
Zhang

Your CGSizeZero is probably causing your date picker to not show.

This is how I do it in a simple way:

.H file

@interface ViewController : UIViewController

@property (nonatomic, strong) UIDatePicker *datePicker;
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UIButton *btnShowPicker;

@end

.M file

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [self initViews];
}

-(void)initViews
{
    self.datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 20, 280, 200)];

    // hide date picker at first
    self.datePicker.alpha = 0;


    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(20, self.datePicker.frame.origin.y + self.datePicker.frame.size.height + 20, self.view.frame.size.width - 40.0, 35)];
    self.textField.layer.borderWidth = 1.0;
    self.textField.layer.borderColor = [UIColor grayColor].CGColor;
    self.textField.textAlignment = NSTextAlignmentCenter;


    self.btnShowPicker = [[UIButton alloc] initWithFrame:CGRectMake(20, self.view.bounds.size.height - 140.0, self.view.frame.size.width - 40.0, 50.0)];
    [self.btnShowPicker setTitle:@"Show Date Picker" forState:UIControlStateNormal];
    self.btnShowPicker.backgroundColor = [UIColor blueColor];
    [self.btnShowPicker addTarget:self action:@selector(showDatePicker:) forControlEvents:UIControlEventTouchUpInside];


    self.btnDone = [[UIButton alloc] initWithFrame:CGRectMake(self.btnShowPicker.frame.origin.x, self.btnShowPicker.frame.origin.y + self.btnShowPicker.frame.size.height + 10.0, self.btnShowPicker.frame.size.width, self.btnShowPicker.frame.size.height)];
    self.btnDone.backgroundColor = [UIColor greenColor];
    [self.btnDone setTitle:@"Done" forState:UIControlStateNormal];
    [self.btnDone addTarget:self action:@selector(datePicked) forControlEvents:UIControlEventTouchUpInside];
    self.btnDone.alpha = 0;


    [self.view addSubview:self.datePicker];
    [self.view addSubview:self.textField];
    [self.view addSubview:self.btnShowPicker];
    [self.view addSubview:self.btnDone];
}

-(void)showDatePicker:(id)sender
{
    self.datePicker.alpha = 1.0;
    self.btnDone.alpha = 1.0;
}

-(void)datePicked
{
    if(self.datePicker.alpha != 0)
    {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"dd/MM/yyyy HH:mm:ss"];

        // populate the text field with new date
        self.textField.text = [dateFormatter stringFromDate:self.datePicker.date];
    }
}

Now when you scroll the date picker, you get the date populated in your text field like this:

screenshot

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to set default selected value in Angular Chosen?

How to set the default value in datetimepicker as previous month

How to set dynamic value to minView in datetimepicker?

How do I change the button title with the date value after a date is chosen from React-Native DateTimePicker?

How to show Button Value in TextField in Flutter?

time wont show time in textfield with datetimepicker

How to set page numbering to start at chosen value in Word

How do I reset the value of the textfield based on what chosen on the radio button?

How to set value to second page textfield from Main page textfield

How to set value of a Date Textfield in Material UI

how to set value style in center (at TextField Flutter)

How to set grouped TextField value to the state?

Show value of option when chosen on select in Angular

Show the default value chosen for XX:ParallelGCThreads

How to set initial(default) value in moment dateTimePicker with AngularJS

how to set up datetimepicker

Set value of textfield with javascript

Flutter set value to a textfield

react set value to a textfield

How to Get Chosen typed value

Datetimepicker set value of different format

Assigning value chosen from a UIPicker to a UITextField in Swift (also how to show and hide it)

rails 3 collection with chosen-select how to show the right value on edit

How to show the selected value in input textfield in material ui autocomplete?

How to Show/Hide some elements based on value of a textfield?

How to show value, when using date type in TextField?

Logic to show how many times a button is chosen

Foundation for Wordpress - How to show text if no menu is chosen?

How do you set a value in codebehind depending on which radio button is chosen?