Title: Custom keyboard clear button
Tags: iphone;iphone-softkeyboard
Question: 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?


Here is the accepted answer: 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&gt; 

    #define kBackSpaceRepeatDelay 0.1f
    #define kBackSpacePauseLengthBeforeRepeting 0.2f

    @interface clearontapAppDelegate : NSObject <UIApplicationDelegate&gt; {
        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 &gt; 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
```

Comment for this answer: Ok, now I understand.  I wrote a sample app to do that and replaced my post with it above.
Comment for this answer: The above code will clear all texts at a single tap. I need exactly,what iPhone keyboards clear button does. That is at a single tap delete only one character, and on continous click clear all contents.
Comment for this answer: very nice!!! Thank you very much... Now i got what i exaclty want. Thanks again!!!

Here is another answer: 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)

Comment for this answer: Touchdown repeat will delete character on clicking button two times. But i am not leaving control from my button. Actually it is a single tap .

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 2.5', 'CreationDate': '2009:00:10/02/09', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<iphone><iphone-softkeyboard>', 'Title': 'Custom keyboard clear button', 'ViewCount': 1281, 'id': 1507731, 'user_AboutMe': None, 'user_AccountId': 561069.0, 'user_DisplayName': 'diana', 'user_Reputation': 427.0, 'user_Views': 111.0, 'user_id': 150314.0}}