Hacer Akac
Hacer Akac

Reputation: 175

UITextField shouldChangeCharactersInRange method in objective c

I have some textFields and when I write 11111111111 to the first textfield (TCKimlikText) I set some strings to the other textfields. Also first text field's (TCKimlikText) max lenght must be 11 character. I try this below code. But I have one problem. I can not delete which I write to TCKimlikText textfield.

- (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;


}

How can I solve this?

Upvotes: 1

Views: 7549

Answers (3)

Akash Raghani
Akash Raghani

Reputation: 557

Use this code u achive your target 

Here your texfiled not allow to enter more then 15 words so this is the validation./..

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

if (textField == txtFirstName)
{
    NSUInteger newLength = [txtFirstName.text length] + [string length] - range.length;
    return (newLength > 15) ? NO : YES;
}
}

Upvotes: 0

Apurv
Apurv

Reputation: 17186

The best way to is following:

-(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;

}

This will also help you while someone paste the text into your text field.

Upvotes: 2

Darren
Darren

Reputation: 10398

You can detect a delete/backspace using

if ([string length] > 0)

If thats not what you meant, if you mean you can change in the other text field, you can make shouldChangeCharactersInRange only work on certain fields using

if (textField == self.myField)

Upvotes: 2

Related Questions