ios - shouldChangeCharactersInRange 没有被调用。?

标签 ios objective-c uitextfield

我有必须调用的方法来检查手机号码范围和其他格式。但这个方法没有被调用:(

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    ProfileBasicCell *cell=[_tblView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    if (cell.txtMobile.text== textField && string.length>0) {
        NSString *newText=[textField.text stringByReplacingCharactersInRange:range withString:string];
        if (newText.length<=15) {
            if (newText.length==4 || newText.length==8) {
               textField.text=[newText stringByAppendingString:@"-"];
               return NO;
             }
        return YES;
    } else {
        return NO;
    }  
   }
   return YES;
}

最佳答案

你设置了委托(delegate)吗?

class yourViewController: UITextFieldDelegate {

}

self.yourTextField.delegate = self;

关于ios - shouldChangeCharactersInRange 没有被调用。?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40677054/

相关文章:

ios - 使用 prepareforsegue 的 XCode 5 调用类

ios - 具有可选函数实现的 Swift @objc 协议(protocol)会导致构建错误

iphone - 在项目 iPhone 中获取已保存视频文件的 UITableView 列表

android - 如何用\n替换每一行

ios - iPhone 可以有 "None"位置 - 开启 GPS

iOS 应用程序不与动态链接 URL 前缀关联

iphone - 当你在 iPhone 上输入完文字后,如何关闭键盘?

ios - TextField 在编辑 Swift 4 时消失

ios - 在 Objective C 中检查 Swift ViewController 的 isKindOfClass 方法

ios - 如何访问 XLFormRowDescriptor 的占位符文本颜色