In my application i create a custom Numberpad .How can i delete whole contents of the textfield on continuos tap on the clear button. Any idea?
This was a fun one.
Basically what I did was write a method to pull the last character out of the textField's text String.
I added another method to be fired on the Button's touchDown event, which first calls the method to erase the last character and then starts a timer to start repeating. Because the delay before repeating (at least on the native keyboard) is longer than repeat delay, I use two timers. The first one's repeat option is set to NO. it calls a method that starts a second timer that repeats to call the erase last character method repeatedly.
In addition to the touchDown event. We also register for the touchUpInside event. When fired it calls a method that invalidates the current timer.
#import <UIKit/UIKit.h>
#define kBackSpaceRepeatDelay 0.1f
#define kBackSpacePauseLengthBeforeRepeting 0.2f
@interface clearontapAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
NSTimer *repeatBackspaceTimer;
UITextField *textField;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) NSTimer *repeatBackspaceTimer;
@end
@implementation clearontapAppDelegate
@synthesize window,
@synthesize repeatBackspaceTimer;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 40, 300, 30)];
textField.backgroundColor = [UIColor whiteColor];
textField.text = @"hello world........";
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(10, 80, 300, 30);
button.backgroundColor = [UIColor redColor];
button.titleLabel.text = @"CLEAR";
[button addTarget:self action:@selector(touchDown:) forControlEvents:UIControlEventTouchDown];
[button addTarget:self action:@selector(touchUpInside:) forControlEvents:UIControlEventTouchUpInside];
// Override point for customization after application launch
[window addSubview:textField];
[window addSubview:button];
window.backgroundColor = [UIColor blueColor];
[window makeKeyAndVisible];
}
-(void) eraseLastLetter:(id)sender {
if (textField.text.length > 0) {
textField.text = [textField.text substringToIndex:textField.text.length - 1];
}
}
-(void) startRepeating:(id)sender {
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:kBackSpaceRepeatDelay
target:self selector:@selector(eraseLastLetter:)
userInfo:nil repeats:YES];
self.repeatBackspaceTimer = timer;
}
-(void) touchDown:(id)sender {
[self eraseLastLetter:self];
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:kBackSpacePauseLengthBeforeRepeting
target:self selector:@selector(startRepeating:)
userInfo:nil repeats:YES];
self.repeatBackspaceTimer = timer;
}
-(void) touchUpInside:(id)sender {
[self.repeatBackspaceTimer invalidate];
}
- (void)dealloc {
[window release];
[super dealloc];
}
@end
On touchUpInside, delete a single character.
On touchDownRepeat, delete the whole word or field (I think the iPhone's delete deletes a word at a time first)
来源:https://stackoverflow.com/questions/1507731/custom-keyboard-clear-button