Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextView as own delegate means infinite loop

I have a subclass of a UITextView and I would like to make it it's own delegate. So that I can override the textView:shouldChangeTextInRange:replacementText: method, and prevent consecutive spaces being entered.

In [SATextView awakeFromNib] (SATextView is my subclass of UITextView), I do [self setDelegate:self];. When I press on the textview to start editing everything freezes and eventually stops, and that backtrace shows that there has been an infinite loop.

It doesn't matter if I implement all delegate methods, just one, or none. Nor does it matter if those methods are empty.

Why does this cause an infinite loop? It only seems to happen if with UITextView (other objects you can subclass and set the delegate to self, and it won't have this problem). And how can I stop it? Or is there a better way to have this subclass not able to have consecutive spaces,

like image 451
Jonathan. Avatar asked Nov 14 '22 03:11

Jonathan.


1 Answers

An idea... You could make a delegate Class that acts as a middle man between the real delegate and the UITextView (because you'll probably need to set the delegate after some time). So this new class will implement the delegate protocol, but it will also have a property for it's own delegate, so that you can forward textView:shouldChangeTextInRange:replacementText:, and still do the work of editing the spaces in your middleMan class.

like image 189
Kpmurphy91 Avatar answered Nov 16 '22 02:11

Kpmurphy91