我不确定在点击时是否可以在UITextField下动态添加DatePicker。如果可能,请帮助我显示该示例代码。

最佳答案

在文本字段上设置的单击事件

- (void)viewDidLoad {
    [super viewDidLoad];

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


设置文本字段版本为false

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


单击文本字段时的方法

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


当日期值更改时,此方法称为

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


希望对您有帮助! :)

关于ios - 如何在UITextField下动态添加DatePicker,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31261462/

10-11 09:04