BlueAroma

UITextField - 키보드에서 삭제 버튼 클릭 여부 인식하기 본문

내맘대로 프로그래밍/iOS

UITextField - 키보드에서 삭제 버튼 클릭 여부 인식하기

BlueAroma 2017. 9. 7. 13:15




UITextField 를 카테고리 한 후  Private한 함수를 하나 override 함. 

( 앱스토어 검수에서 문제가 생기지 않았다고 함 )



UITextField+delete.h


 

@interface UITextField(delete)


@end





UITextField+delete.m



@implementation UITextField(delete)


- (BOOL)keyboardInputShouldDelete:(UITextField *)textField {


    NSInteger location = [textField text].length - 1;

    

    if ([self.delegate respondsToSelector:@selector(textField:shouldChangeCharactersInRange:replacementString:)]) {

        [self.delegate textField:self shouldChangeCharactersInRange:NSMakeRange(location, 1) replacementString:@""];

    }

    

    return YES;

}



@end







활용하기


Delete 버튼 인식할 UITextField 는 Delegate를 연결한다.


ViewController.m




- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    

    if(range.length == 1 && string.length == 0) {

//- backspace

    }

    

    return  YES;

}




Comments