我有UITextField
。我将UIDatePicker
设置为此UITextField
UITextField *dateField = [[UITextField alloc] init];
dateField.frame = CGRectMake(30, 180, 260, 30);
dateField.delegate = self;
[self addSubview:dateField];
UIDatePicker *datePicker = [[UIDatePicker alloc]init];
[datePicker setDate:[NSDate date]];
datePicker.frame = CGRectMake(0, 568 - 100, 320, 100);
[datePicker addTarget:self action:@selector(updateTextField:) forControlEvents:UIControlEventValueChanged];
[dateField setInputView:datePicker];
-(void)updateTextField:(id)sender
{
NSLog(@"SENDER = %@", sender);
}
有没有办法在UITextField
方法
updateTextField
答案 0 :(得分:2)
是的,您可以像这样引用textView:
-(void)updateTextField:(id)sender
{
UIDatePicker *datePicker = (UIDatePicker*) sender;
UITextField *dateField = (UITextField*)datePicker.inputView;
}
//扩展
抱歉我的错误我认为您将文本字段添加为数据选择器的输入视图。 您可以执行所需操作的另一种方法是将标记分配给您设置它的文本视图:
UITextField *dateField = [[UITextField alloc] init];
dateField.frame = CGRectMake(30, 180, 260, 30);
dateField.tag = 10;
dateField.delegate = self;
[self addSubview:dateField];
在您的方法updateTextField中:您可以像访问它一样访问它:
UITextField *dateField = (UITextField*)[self viewWithTag:10];
答案 1 :(得分:1)
添加此项并创建ivar currentTextField
- (void)textFieldDidBeginEditing:(UITextField *)textField {
currentTextField = textField;
}
updateTextField中的currentTextField将是您的文本字段