Limit copying a row insert in a numeric text field

I have a text box in which I wanted the user to enter only numbers. I have introduced a numeric keypad. But if someone puts my application in the background and copies some character string from another application and returns to my application and pastes it, it successfully inserts the contents of the string into my numeric text box. How can I limit this scenario?

+3
source share
2 answers

@theChrisKent is close, but there is a slightly better way. Use the delegate method -textView:shouldChangeTextInRange:replacementText:. Make sure it replacementTextcontains any non-numbers, and if so, return it NO.

+6
source

, : , , , UITextView

UITextView (, ):

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(paste:)
        return NO;
    return [super canPerformAction:action withSender:sender];
}

UITextViewDelegate textViewDidChange: , . , . : http://developer.apple.com/library/ios/documentation/uikit/reference/UITextViewDelegate_Protocol/Reference/UITextViewDelegate.html#//apple_ref/occ/intfm/UITextViewDelegate/textViewDidChange:

0

Source: https://habr.com/ru/post/1791325/


All Articles