1

私のアプリケーションでは、カスタム Numberpad を作成します。クリアボタンを連続してタップすると、テキストフィールドの内容全体を削除するにはどうすればよいですか。何か案が?

4

2 に答える 2

1

これは楽しいものでした。

基本的に私がしたことは、textField のテキスト文字列から最後の文字を取り出すメソッドを作成することでした。

ボタンの touchDown イベントで発生する別のメソッドを追加しました。このメソッドは、最初にメソッドを呼び出して最後の文字を消去し、次にタイマーを開始して繰り返しを開始します。(少なくともネイティブ キーボードでは) 繰り返すまでの遅延は、繰り返しの遅延よりも長いため、2 つのタイマーを使用します。最初の繰り返しオプションは NO に設定されています。2 番目のタイマーを開始するメソッドを呼び出します。このタイマーは、最後の文字の消去メソッドを繰り返し呼び出すために繰り返されます。

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
于 2009-10-02T04:56:50.707 に答える
0

touchUpInside で、1 文字削除します。

touchDownRepeat で、単語またはフィールド全体を削除します (iPhone の削除では、最初に一度に単語が削除されると思います)。

于 2009-10-02T05:19:52.453 に答える