目标c中的UITextField shouldChangeCharactersInRange方法

时间:2012-01-27 07:55:41

标签: objective-c ios uitextfield

我有一些textFields,当我将11111111111写入第一个文本字段(TCKimlikText)时,我将一些字符串设置为其他文本字段。第一个文本字段(TCKimlikText)最大长度必须是11个字符。我试试下面的代码。但我有一个问题。我无法删除我写入TCKimlikText文本字段的内容。

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


if (TCKimlikText.text.length <11) {

    if ([[TCKimlikText.text stringByReplacingCharactersInRange:range withString:string] isEqual:@"11111111111"])
    {   
        AdinizText.text = @"MEHMET";
        uyrukBtn.titleLabel.text = @"TC";
        BabaAdiText.text = @"HASAN";
        AnneAdiText.text = @"MELEK";
        SoyadinizText.text = @"AYDIN";
        AnneKizlikSoyadiText.text = @"OKUR";
        DogumTarihiText.text = @"01/01/1960";
        medeniDurumBtn.titleLabel.text = @"EVLİ";
        EsinizinAdiText.text = @"AYŞE";
        dogumYeriBtn.titleLabel.text = @"BURSA";
        kimlikTuruBtn.titleLabel.text = @"Nüfus Cüzdanı";
        KimlikVerilisNedeniText.text = @"YENİLEME";
        KimlikVerilisTarihi.text = @"12/01/2010";
        KimlikSeriNoText.text = @"16345";
        VergiKimlikNoText.text = @"45678";
        EvTelText.text = @"5443126274";
    }
    return  YES;


}

else  return NO;


}

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

最好的方法是:

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

NSString *resultStr = [TCKimlikText.text stringByReplacingCharactersInRange:range withString:string];

if (resultStr.length < 12)
{

    if ([resultStr isEqual:@"11111111111"])
    {   
        NSLog(@"In");
        AdinizText.text = @"MEHMET";
        uyrukBtn.titleLabel.text = @"TC";
        BabaAdiText.text = @"HASAN";
        AnneAdiText.text = @"MELEK";
        SoyadinizText.text = @"AYDIN";
        AnneKizlikSoyadiText.text = @"OKUR";
        DogumTarihiText.text = @"01/01/1960";
        medeniDurumBtn.titleLabel.text = @"EVLİ";
        EsinizinAdiText.text = @"AYŞE";
        dogumYeriBtn.titleLabel.text = @"BURSA";
        kimlikTuruBtn.titleLabel.text = @"Nüfus Cüzdanı";
        KimlikVerilisNedeniText.text = @"YENİLEME";
        KimlikVerilisTarihi.text = @"12/01/2010";
        KimlikSeriNoText.text = @"16345";
        VergiKimlikNoText.text = @"45678";
        EvTelText.text = @"5443126274";
    }
    return  YES;        

}

else  return NO;

}

当有人将文字粘贴到您的文字字段时,这也会对您有所帮助。

答案 1 :(得分:2)

您可以使用

检测删除/退格
if ([string length] > 0)

如果那不是你的意思,如果你的意思是你可以在另一个文本字段中更改,你可以使用shouldChangeCharactersInRange只对某些字段使用

if (textField == self.myField)