与 UITextfield 的委托方法 shouldChangeCharactersInRange 并行使用时,不会触发 UIControlEventEditingChanged

UIControlEventEditingChanged doesn't get fired when used parallel with delegate method shouldChangeCharactersInRange of UITextfield

我在获取文本字段的 UIControlEventEditingChanged 事件时遇到问题。

代码如下:

[_txtNum1 addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

然后这个方法:

 -(void)textFieldDidChange :(UITextField *) textField{}

当我没有实现文本字段的以下委托方法时,这完美地调用了 textFieldDidChange

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

当我并行使用文本字段的委托方法 shouldChangeCharactersInRange 时,它不会为 textFieldDidChange 触发事件,而只会为 shouldChangeCharactersInRange.

触发事件

在此先感谢您的帮助。

你必须设置

 self.textfield.delegate = self;

如果此方法返回 NO,则不会调用 change 方法

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    return YES;
}

@DhavalBhimani 我刚刚创建了一个新应用程序并使用一个文本字段并将委托分配给它。只需复制您的代码并将其粘贴到我的演示中即可。它似乎在工作。你可以检查下面的代码。也许你错过了什么..

- (void)viewDidLoad {
    [super viewDidLoad];
    [_txtDemo addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}

-(void)textFieldDidChange:(UITextField *)textField
{
    NSLog(@"called");
}

#pragma mark - textfield methods

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSLog(@"string = %@",string);
    return YES;
}

2018-03-24 00:27:35.292771+0530 Demo Objective C[783:14964] string = h

2018-03-24 00:27:35.382728+0530 Demo Objective C[783:14964] called

2018-03-24 00:27:39.851133+0530 Demo Objective C[783:14964] string = i

2018-03-24 00:27:39.869326+0530 Demo Objective C[783:14964] called