怎么实现这个代理。
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string :(id)sender
{
    NSCharacterSet *cs;
    //textField.delegate = self;
    if(textField == phone)
    {
        NSLog(@"phone 数字");
        cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS] invertedSet];
        NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        BOOL basicTest = [string isEqualToString:filtered];
        if(!basicTest)
        {
            UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                            message:@"请输入数字"
                                                           delegate:nil
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];
            
            [alert show];
            //[alert release];
        return NO;
        }
        
    }
   return YES;
    //其他的类型不需要检测,直接写入
   //return YES;
}这个委托是放在.m文件实现还是再建立个类别,在xib文件中还需要设置什么吗?
简单来说就是实现这个判断委托。

解决方案 »

  1.   

    给你的UITextField设置个delegate就行了,放在同一个.m里也可以的。
      

  2.   

    .m 或 .h 加入协议 <UITextFieldDelegate>
    UITextField.delegate = self;
      

  3.   

    添加当文本框输入改变时的通知 [[NSNotificationCenter defaultCenter] addObserver:self 
            selector:@selector(textUpdated)
            name: UITextFieldTextDidChangeNotification
            object:self.inputValueField];