2009-10-02 2 views
1

내 응용 프로그램에서 사용자 지정 숫자 패드를 만듭니다. 지우기 단추의 continuos 탭에서 텍스트 필드의 전체 내용을 어떻게 삭제할 수 있습니까? 어떤 생각?사용자 지정 키보드 지우기 단추

답변

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 
+0

위의 코드는 하나의 탭에서 모든 텍스트를 삭제합니다 (I은 아이폰의이 삭제 생각은 처음 한 번에 단어를 삭제합니다). iPhone 키보드에서 버튼을 지우는 것이 정확히 필요합니다. 한 번 탭하면 한 문자 만 지우고 계속 클릭하면 모든 내용이 지워집니다. – diana

+0

이제 알았습니다. 그 일을하기위한 샘플 앱을 작성하고 위의 게시물을 대체했습니다. –

+0

아주 좋은 !!! 고마워 ... 이제 내가 원하는게있어. 다시 한 번 감사드립니다! – diana

0

터치 업하면 한 문자를 삭제하십시오. touchDownRepeat에

, 전체 단어 또는 필드 삭제

+0

터치 다운 반복은 버튼을 두 번 누르면 문자가 삭제됩니다. 그러나 나는 버튼에서 컨트롤을 떠나지 않을 것이다. 실제로 그것은 단일 탭입니다. – diana