我有一个UITextField会显示一些像“rocky”这样的值。如果我从文本字段中删除此文本,那么我希望字段文本立即更改回原始文本。
答案 0 :(得分: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。
答案 1 :(得分: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;
}
答案 2 :(得分: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;
}
答案 3 :(得分:0)
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(textField.length==0) {
txtProjectName.text=@"rocky";
return NO;
}
return YES;
}
试试这个。