Search code examples
iosobjective-cuitextfielduitextfielddelegate

shouldChangeCharactersInRange not called from class inheriting from UITextField


I know there are posts regarding shouldChangeCharactersInRange but none of them target my problem. So this Is what I am struggling with. I have a class kMoneyTextField which inherits from UITextField. I want to parse every character typed by user before I print it inside TextField. Unfortunatelly my shouldChangeCharactersInRange method is not being called - NEVER!

kMoneyTextField.h

@interface kMoneyTextField : UITextField <UITextFieldDelegate>
@end

kMoneyTextField.m

@implementation kMoneyTextField

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

-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    // WHY NOBODY CALLS ME?!?
    return YES;
}

I am out of ideas and I really need this method to work. I am working with xcode 6.4 targeting ios7.0+. Thanks in advance for your time and help.


Solution

  • I'm going to guess that someone else is setting themselves as the delegate after you do it, thus pulling the delegate messages away from you.