user1525984
user1525984

Reputation:

How to get UITextView text while user is still editing it

I use
[self.assuntoTextField addTarget:self action:@selector(testarSeTemTexto) forControlEvents:UIControlEventEditingChanged];

to get the text value of the UITextFields, but I can't do the same with UITextViews, how can I do that WHILE user is still editing the TextView?

Upvotes: 0

Views: 1100

Answers (2)

Prince Agrawal
Prince Agrawal

Reputation: 3607

Make your viewController a delegate of textView like this:

@interface ViewController : UIViewController<UITextViewDelegate>

This is your delegate method:

- (void)textViewDidChange:(UITextView *)textView

See this for more info

Upvotes: 1

Rok Jarc
Rok Jarc

Reputation: 18865

If you don't need validation and simply want to be informed that the text had been changed simply assign a delegate to this UITextField (if yiu haven't done this yet) and implement a method

- (void)textViewDidChange:(UITextView *)textView
{
    NSString *text = textView.text;
}

If you want to implement some kind of validation then the proper method to use would be

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    NSString *finalTextWillBe = [textField.text stringByReplacingCharactersInRange:range
                                                                        withString:string];

    //validation comes here... 

    if (final text passes your validation) 
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

NOTE: if you are using more than one text field you will have to identify the source somehow. Either by setting its tag or by holding a reference (ivar, property...) to the text fields.

Upvotes: 5

Related Questions