删除前一输入的字符
@implementation UITextFieldBack
-(void)myDeleteBackward {
if ([selfconformsToProtocol:@protocol(UITextInput)]){
// iOS5 andlater
[selfdeleteBackward];
// Or dobelow line if you are not deploy-targeting 5.0 or above and want to avoidwarnings
//[textFieldperformSelector:@selector(deleteBackward)];
} else {
@try{
//checkcurrent selected range
NSRangeselectedRange = [[self valueForKey:@"selectionRange"]rangeValue];
if(selectedRange.location == NSNotFound){
selectedRange = NSMakeRange([[selftext]length],0);
}
if(selectedRange.location < 1){
return;
}
//deleteone char
NSRangedeleteRange = (selectedRange.length >0) ? selectedRange : NSMakeRange(selectedRange.location - 1, 1);
self.text = [self.textstringByReplacingCharactersInRange:deleteRangewithString:@""];
//adjustthe selected range to reflect the changes
selectedRange.location = deleteRange.location;
selectedRange.length =0;
[selfsetValue:[NSValuevalueWithRange:selectedRange]forKey:@"selectionRange"];
} @catch(NSException *exception) {
NSLog(@"deleteBackword failed but catched. %@",exception);
} @finally{}
}
}
@end
TextField只要有输入,马上清掉旧值
@interface UITextFieldEx :UITextField
{
BOOL deleteFirst;
}
@property(nonatomic)BOOL deleteFirst;
@end
@implementation UITextFieldEx
@synthesize deleteFirst;
-(id)initWithFrame:(CGRect)frame
{
self = [superinitWithFrame:frame];
if (self) {
deleteFirst =YES;
}
returnself;
}
@end
在textFieldDelegate中实现:
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
UITextFieldEx*field = (UITextFieldEx *)textField;
field.deleteFirst=YES;
}
-(BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)rangereplacementString:(NSString *)string
{
UITextFieldEx*field = (UITextFieldEx *)textField;
if(field.deleteFirst) {
field.text=@"";
field.deleteFirst=NO;
}
returnYES;
}