Как обнаружить пользовательский ввод символов в UITextView - PullRequest
15 голосов
/ 21 марта 2012

Как обнаружить символ ввода пользователя в UITextView?

Например: (в UITextView NOT UITextField)

когда пользователь вводит букву «а», инициирует событие.когда пользовательский ввод "сделать это", вызвать другое событие.или ввод "http://www.stackoverflow.com" вызвать событие.

Спасибо

Ответы [ 4 ]

26 голосов
/ 21 марта 2012

Вы можете отслеживать изменения содержимого TextView внутри этого метода делегата и запускать необходимые действия.

- (void)textViewDidChange:(UITextView *)textView
{
    //Access the textView Content
    //From here you can get the last text entered by the user
    NSArray *stringsSeparatedBySpace = [textView.text componentsSeparatedByString:@" "];
    //then you can check whether its one of your userstrings and trigger the event
    NSString *lastString = [stringsSeparatedBySpace lastObject];
    if([lastString isEqualToString:@"http://www.stackoverflow.com"]) //add more conditions here
    {
          [self callActionMethod];
    }
}
11 голосов
/ 21 марта 2012

Этот делегат будет вызываться всякий раз, когда пользователь нажимает клавишу

Попробуйте: -

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if([[textView.text stringByAppendingString:text] isEqualToString:@"a"])
    {
      //trigger 'a' operation
    }
    else if([[textView.text stringByAppendingString:text] isEqualToString:@"do it"])
    {
      //trigger do it operation
    }
    //Same conditions go on
}
8 голосов
/ 21 января 2017

Рабочий раствор Swift 3

Сначала добавьте UITextViewDelegate

Затем установите делегата в viewDidLoad следующим образом: self.testTextField.delegate = self

И, наконец, вызовите функцию textViewDidChange, пример ниже.

func textViewDidChange(_ textView: UITextView) {
    // Do the logic you want to happen everytime the textView changes
    // if string is == "do it" etc....

}
0 голосов
/ 21 марта 2012
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{
    NSString * length;   
    NSString *currentString = [textField.text stringByReplacingCharactersInRange:range withString:string];
length = [currentString length];
if (length > 1)
{

    looprange = _looptext.text;
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Please ! "
                                        message:@"Insert Single Characters !!"
                                                   delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
    return NO;

}
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...