Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextView shouldChangeTextInRange delegate not called

I am using this code to set the parameters of an uitextview i have on 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 in the .h this code

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

I have connected the outlet to the uitextview by using the interface.

What happens is the following:

- (void)textViewDidChange:(UITextView *)inTextView

the above delegate is called but not the following one:

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

Why is this happening? Am i doing something wrong?

Any help appreciated...

like image 272
stefanosn Avatar asked Feb 13 '12 00:02

stefanosn


2 Answers

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

The <UITextViewDelegate> method is:

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

The correct method signature is:

textView:shouldChangeTextInRange:replacementText:

not:

textView:shouldChangeTextInRange:replacementString:
like image 95
NJones Avatar answered Oct 04 '22 20:10

NJones


Because you are typing the method name wrong.

There is no

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

but only

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

careful with the different between replacementString and replacementText.

Also, as the others said, if you've created the UITextView in IB, you should NOT init it again in your code (but this is not the reason why your delegate never invoked).

like image 42
xuzhe Avatar answered Oct 04 '22 21:10

xuzhe