ShouldChangeCharactersInRange is not work in custom Textfield

25 views Asked by At

I am a novice iOS developer and not very familiar with the iOS development process. I have encountered an issue with a custom TextField, inherited in iOS using Objective-C, where it lacks the shouldChangeCharactersInRange method. Any guidance or assistance in understanding the iOS development workflow and resolving this issue would be greatly appreciated.

//  CJTextField.h
#import <UIKit/UIKit.h>

@class CJTextField;

@protocol CJTextFieldDeleteDelegate <NSObject>

- (void)cjTextFieldDeleteBackward:(CJTextField *)textField;

//- (void)cjTextFieldChangeText:(CJTextField *)textField;

- (void)switchToNextTextFieldFromTextField:(CJTextField *)textField;

@end

@interface CJTextField : UITextField <UITextFieldDelegate>

@property (nonatomic, weak) id <CJTextFieldDeleteDelegate> cj_delegate;

@end


// CJTextField.m
#import <Foundation/Foundation.h>
#import "CJTextField.h"

@implementation CJTextField

@synthesize cj_delegate;


#pragma mark - UITextFieldDeleteBackward

- (void)deleteBackward {

    if ([self.cj_delegate respondsToSelector:@selector(cjTextFieldDeleteBackward:)]) {
        [self.cj_delegate cjTextFieldDeleteBackward:self];
    }
    [super deleteBackward];
}

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(CJTextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSLog(@"currentText%@",currentText);
    return YES;
}

I attempted to set self.smsCodeField.cj_delegate = self; inside the Controller calling CJTextField.m, and tried invoking shouldChangeCharactersInRange, but it's not taking effect as expected.

//viewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.smsCodeField.cj_delegate = self;
}

#pragma mark - UITextFieldDelegate

- (BOOL)textField:(CJTextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSLog(@"currentText%@",currentText);
}
0

There are 0 answers