Mishal Awan
Mishal Awan

Reputation: 724

how to show datetimepicker and set the chosen value in textfield

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]]);
}

Upvotes: 0

Views: 590

Answers (1)

Zhang
Zhang

Reputation: 11607

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

Upvotes: 1

Related Questions