【发布时间】:2013-12-29 22:05:12
【问题描述】:
我以编程方式创建了 2 个 UITextfield,我想在编辑之前清除它们。问题是当我点击 UITextfield 时,它们清晰可见(我用 NSLog 检查它)但视图没有更新。
当我用键盘编辑文本字段时,新字符串似乎附加了现有字符串(而不是清除和编辑),但实际上文本字段没有编辑。
我已阅读与此问题相关的所有论坛,但找不到解决方案。这似乎是 UITextFieldDelegate 的线程问题...
请注意,UITextField 嵌入在滚动视图中。
我已经在我的文本字段上尝试了 setNeedsDisplay、clearsOnEditing 属性、将代码放入“textFieldsShouldBeginEditing”和“textFieldsDidBeginEditing”方法以及许多其他的方法中,但都没有成功。一些帮助会很酷!
与文本字段相关的代码:
@implementation gaineViewController
- (UITextField *)hauteurText {
if(!_hauteurText){
_hauteurText = [[UITextField alloc]init];
_hauteurText.clearsOnBeginEditing = YES;
_hauteurText.delegate = self;
_hauteurText.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
}
return _hauteurText;
}
- (UITextField *)largeurText {
if(!_largeurText){
_largeurText = [[UITextField alloc]init];
_largeurText.clearsOnBeginEditing = YES;
_largeurText.delegate = self;
_largeurText.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
}
return _largeurText;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if ([textField isEqual:self.largeurText]) {
NSLog(@"%@",self.largeurText.text);
self.largeurText.text=@"";
NSLog(@"%@",self.largeurText.text);
}
if ([textField isEqual:self.hauteurText]) {
NSLog(@"%@",self.hauteurText.text);
self.hauteurText.text=@"";
NSLog(@"%@",self.hauteurText.text);
}
return YES;
}
@end
【问题讨论】:
-
您是否尝试过检查 textField == self.hauteurText。这是有效的,因为您实际上正在查看相同的确切实例(身份重要而不是平等)。
-
您发布的代码没有问题。发布更多。
-
您实际上是在哪里/如何将文本字段放入视图中?
标签: ios uitextfield uitextfielddelegate