2

I've subclassed UITextView and want to override the shouldChangeTextInRange delegate function to handle maximum number of lines. So I have the following setup:

@interface CustomFieldView : UITextView <UITextViewDelegate>

Then in the .m I set my subclasses delegate to itself within initWithCoder as I am loading the view from IB:

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];

    if (self)
    {
        self.textContainer.lineFragmentPadding = 0;
        self.textContainerInset = UIEdgeInsetsZero;

        self.constraintHeight = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:0];

        [self setScrollEnabled:NO];

        self.delegate = self;
    }

    return self;
}

I am using attributed text, however, for debug reasons I am also setting the regular text incase this was causing issues:

[self setAttributedText:string];
[self setText:[string string]];

This works as expected and the text is set, however, I have a break point set in the delegate function:

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

But this is never called, can anyone let me know why this would be? I have not set the delegate in any of the other calling functions or IB, so I know this is being set. If I break in to the function that sets the text and po the delegate I get the following:

(lldb) po self.delegate
<CustomFieldView: 0x7fc01b099a00; baseClass = UITextView; frame = (8 79.5; 584 49); text = ' • Desired Sizes: Large '; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; tag = 9; gestureRecognizers = <NSArray: 0x7fc01a5d9000>; layer = <CALayer: 0x7fc01be06840>; contentOffset: {0, 0}; contentSize: {584, 49}>

So it's definitely been set correctly. So, can anyone see any reason as the why the delegate function wouldn't be called? I have also tried the - (void)textViewDidChange:(UITextView *)textView function which isn't called either.

Any guidance on how to override / implement the delegate functions from a subclass is much appreciated.

Thanks, Elliott

Elliott D'Alvarez
  • 1,217
  • 16
  • 30
  • where do you have the delegate method ? in the subclass or in view controller class ? – Teja Nandamuri Jan 04 '16 at 11:44
  • @Mr.T In the subclass, this class appears in multiple VC's so I want the functionality to be consistent between them all. – Elliott D'Alvarez Jan 04 '16 at 11:47
  • is the textview an outlet in storybaord ? – Teja Nandamuri Jan 04 '16 at 11:49
  • try to set the delegate in awakeFromNib method. If you are creating the textfield programmatically, then use initWithFrame instead initWithCoder. – Teja Nandamuri Jan 04 '16 at 11:51
  • @Mr.T I've changed it to awakeFromNib, and set a break point to make sure it's called (which it is). As per the original post if I po the delegate it looks to be set correctly, however, the delegate functions still aren't called. – Elliott D'Alvarez Jan 04 '16 at 11:56
  • r u using the outlet ? – Teja Nandamuri Jan 04 '16 at 11:57
  • @Mr.T Do you mean you want me to use an outlet from a VC to set the delegate? Won't this mean that the implementation of shouldChangeTextInRange will have to happen within the VC? – Elliott D'Alvarez Jan 04 '16 at 11:59
  • no i meant to ask, is the textview created programatically ? – Teja Nandamuri Jan 04 '16 at 12:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/99694/discussion-between-elliott-dalvarez-and-mr-t). – Elliott D'Alvarez Jan 04 '16 at 12:01
  • I would use a separate class for the delegate. The UITextView may have some optimized code that recognizes the case where it is its own delegate and treat that differently. The whole point of delegation is to provide a mechanism to extend the functionality without having to subclass. – Rudi Angela Jan 04 '16 at 15:27
  • Thanks for this, I will create a custom view to hold the delegate instead. Seems a little annoying though, would like the flexibility to do both approaches in an ideal world. – Elliott D'Alvarez Jan 04 '16 at 15:45

0 Answers0