退格键 iOS 的通知 [英] Notifications of backspace key iOS

查看:30
本文介绍了退格键 iOS 的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在用户按下 UITextField 上的退格键时获得通知,即使该字段为空?

Is there a way to get notifications of when the user presses the backspace key on a UITextField, even if the field is empty?

当用户在空字段上退格时,我希望能够触发一些代码.

I would like to be able to trigger some code when the user backspaces on an empty field.

推荐答案

这将检测退格.现在你可以在按下退格键时做任何事情.

This will detect backspace. Now you can do whatever when backspace is pressed.

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

            const char * _char = [string cStringUsingEncoding:NSUTF8StringEncoding];
            int isBackSpace = strcmp(_char, "\b");

            if (isBackSpace == -8) {
                NSLog(@"isBackSpace");

                if (textField.text.length == 9)
                {

                }

                return YES; // is backspace
            }
            else if (textField.text.length == 10) {
                return YES;
            }
        }
        return NO;
    }

这篇关于退格键 iOS 的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆