UITextField自定义输入视图不会被忽略

时间:2014-02-02 12:54:28

标签: ios iphone objective-c uitextfield

我的UITextField的自定义输入视图存在一些问题,我使用了UIDatePickerView,其附件视图是UIButton。我已经解开了解开一个捕获值的方法,将其加载到字段中,然后尝试解除自定义输入视图而没有任何成功......

My Field声明和初始化如下所示

    int width = self.controller.view.frame.size.width;
UITextField* field = [[UITextField alloc] initWithFrame:CGRectMake((width / 100) * 40, 5, (width / 100) * 60, 40)];
self.fieldControl = field;
field.placeholder = @"Select Date..";
field.textColor = self.controller.fieldColor;
self.datePicker = [UIDatePicker new];
field.delegate = self;
self.datePicker.datePickerMode = UIDatePickerModeDate;
UIButton* close = [UIButton buttonWithType:UIButtonTypeSystem];
[close setFrame:CGRectMake(width - 60, 20, 50, 50)];
close.backgroundColor = self.backgroundColor;
[close setTitle:@"Done" forState:UIControlStateNormal];
[close addTarget:self action:@selector(datePicked) forControlEvents:UIControlEventTouchUpInside];
field.inputAccessoryView = close;
field.inputView = self.datePicker;
[self addSubview:field];

我的日期选择方法如下所示

    -(void)datePicked
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"dd/MM/yyyy"];

    NSString *stringFromDate = [formatter stringFromDate:self.datePicker.date];


    UITextView* field = self.fieldControl;
    field.text = stringFromDate;

    [field endEditing:YES];
    [field resignFirstResponder];
}

我无法弄清楚为什么这不起作用,我已经在各种视图上尝试了endEditing方法,似乎没有任何工作。我注意到文本字段确实失去了它,因为它不再显示闪烁的光标...点击另一个uitextfield显示常规键盘,当解雇时关闭我的自定义输入视图...我错过了什么?

1 个答案:

答案 0 :(得分:0)

最好直接在实例变量上将属性分配给UITextField,而不是将它们设置为新的UITextField,并将该文本字段设置为您当前正在执行的实例属性。

或者将fieldControl属性设置为“assign”而不是“strong”,因此不会保留(这可能会导致它在您尝试重新签名时丢失其引用。

像这样:

@property(nonatomic,assign)UITextField * fieldControl;