Pyae Phyoe Shein
Pyae Phyoe Shein

Reputation: 13827

How to add DatePicker dynamically under UITextField

I'm not sure it can be able to add DatePicker dynamically under UITextField when tapped on. If possible, help me to show sample code for that.

Upvotes: 0

Views: 56

Answers (1)

Pravin Tate
Pravin Tate

Reputation: 1130

on click event set on text field

- (void)viewDidLoad {
    [super viewDidLoad];

    [textField addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventEditingDidBegin];
}

set text field edition false

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    return NO;
}

Method when clickd on text filed

 -(IBAction)clicked:(id)sender
    {
        [sender resignFirstResponder];
         picker1   = [[UIDatePicker alloc] initWithFrame:CGRectMake(textField.frame.origin.x-40, textField.frame.origin.y+textField.frame.size.height, textField.frame.size.width, 200)];
        [picker1 setDatePickerMode:UIDatePickerModeDate];
        picker1.backgroundColor = [UIColor whiteColor];
        [picker1 addTarget:self action:@selector(startDateSelected:) forControlEvents:UIControlEventValueChanged];
        [self.view addSubview:picker1];
    }

when date value changed this method is called

-(IBAction)startDateSelected:(id)sender
{
    NSLog(@"%@",picker1.date);
    [textField setText:[NSString stringWithFormat:@"%@",picker1.date
            ]];
}

I hope it will help you! :)

Upvotes: 1

Related Questions