我有一个 UITextField 会显示一些像“rocky”这样的值。如果我从文本字段中删除此文本,那么我希望字段文本立即更改回原始文本。
问问题
209 次
4 回答
1
你需要一点改变,看看我的回答:
- (BOOL)textField:(UITextField *)theTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
if([newString isEqualToString:@""]){
textField.text=@"rocky";
return NO;
}
return YES;
}
您可以注意到差异。只需为您的空字符串案例返回 NO。
于 2012-10-10T12:11:52.100 回答
0
最初添加NSString *strName作为类 object.in viewDidLoad 添加这个
strName = @"Rocky";
现在
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([[string stringByReplacingOccurrencesOfString:@" "
withString:@""] isEqualToString:@""]){
textField.text=strName;
}
else
{
strName = text; //change name here for keeping record
}
return YES;
}
于 2012-10-10T10:55:39.390 回答
0
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
if(newString.length==0) {
txtProjectName.text=@"rocky";
textField.userInteractionEnabled=NO;
return NO;
}
return YES;
}
于 2012-10-10T10:52:45.507 回答
0
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(textField.length==0) {
txtProjectName.text=@"rocky";
return NO;
}
return YES;
}
尝试这个。
于 2012-10-10T12:09:49.563 回答