UITextView shouldChangeTextInRange delegate not called

I use this code to set the uitextview options that I have in my view.

_textview=[[UITextView alloc]init]; [_textview setUserInteractionEnabled:FALSE]; _textview.text=@ "Write your message"; _textview.textColor=[UIColor lightGrayColor]; _textview.layer.cornerRadius=6; _textview.contentInset = UIEdgeInsetsZero; _textview.delegate=self; 

I have this code in .h

 IBOutlet UITextView *_textview; @property(nonatomic,retain) IBOutlet UITextView *_textview; 

I connected the output to uitextview using the interface.

The following happens:

 - (void)textViewDidChange:(UITextView *)inTextView 

The above delegate is invoked, but not the following:

 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementString:(NSString *)string 

Why is this happening? Am I doing something wrong?

Any help appreciated ...

+4
source share
2 answers

In addition to removing _textview = [[UITextView alloc]init]; so as not to overwrite the loaded nib text view.

<UITextViewDelegate> Method:

 -(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{} 

The correct method signature is:

 textView:shouldChangeTextInRange:replacementText: 

not

 textView:shouldChangeTextInRange:replacementString: 
+9
source

Due to an incorrect method name.

Not

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementString:(NSString *)string

but only

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

carefully with the difference between replacementString and replacementText .

Also, as others have said, if you created a UITextView in IB, you should NOT initialize it again in your code (but this is not the reason why your delegate has never been called).

+6
source

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


All Articles