tags:

views:

367

answers:

2

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?

+1  A: 

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
Brad Smith
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.
diana
Ok, now I understand. I wrote a sample app to do that and replaced my post with it above.
Brad Smith
very nice!!! Thank you very much... Now i got what i exaclty want. Thanks again!!!
diana
You are welcome! It would be great if you could accept my answer through the site. That would increase me reputation score and increase your accept rate (which is currently 0%) You should find that with a higher accept score people will put more time into answering your questions faster and more thoroughly.
Brad Smith
A: 

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)

mahboudz
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 .
diana