自定义键盘清除按钮

时间:2009-10-02 04:28:13

标签: iphone iphone-softkeyboard

在我的应用程序中,我创建了一个自定义数字键盘。如何在连续点击清除按钮时删除文本字段的全部内容。有什么想法吗?

2 个答案:

答案 0 :(得分:1)

这很有趣。

基本上我所做的就是编写一个方法来从textField的文本字符串中拉出最后一个字符。

我在Button的touchDown事件中添加了另一个方法,该事件首先调用方法擦除最后一个字符,然后启动一个计时器开始重复。因为重复之前的延迟(至少在本机键盘上)比重复延迟更长,所以我使用两个定时器。第一个重复选项设置为NO。它调用一个方法,启动第二个重复的计时器,重复调用擦除最后一个字符方法。

除了touchDown事件。我们还注册了touchUpInside活动。当被触发时,它会调用一个使当前计时器无效的方法。

    #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

答案 1 :(得分:0)

在touchUpInside上,删除单个字符。

在touchDownRepeat上,删除整个单词或字段(我认为iPhone的删除会一次删除一个单词)

相关问题