P.VenkataVamsi P.VenkataVamsi - 1 month ago 12
Objective-C Question

How to send service call after filling textfields in iOS

I want to send service call to understand whether that pin correct or not. I have 4 textfields. If 4th text field is entered the service call has to go automatically. It means all textfield should fill. The service call has to go in a way that if user changes 1 or 2 or 3 or 4. I know we have to write in

textField:shouldChangeCharactersInRange
. But I don’t know where to write in that.

Please help me. Here is my code:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSCharacterSet *acceptedInput = [NSCharacterSet characterSetWithCharactersInString:ACCEPTABLE_CHARECTERS];

if (textField == self.textFied1&&([[string componentsSeparatedByCharactersInSet:acceptedInput] count] > 0))
{
int length = (int)[textField.text length];
if (length==1)
{
[self.textField2 becomeFirstResponder];
if(range.length == 0)
{
[self.textField2 becomeFirstResponder];
return NO;
}
if([self.textFied1.text length]==1)
{
self.textFied1.text=@"";
[self.textFied1 becomeFirstResponder];
return NO;
}

}
if (([[self.textField4 text] length] > 0)&& ([[self.textField2 text] length] > 0)&& ([[self.textField3 text] length] > 0)&& ([[self.textFied1 text] length] > 0)){
[self connected1];//this step is not executing

}
}
else if (textField == self.textField2&&([[string componentsSeparatedByCharactersInSet:acceptedInput] count] > 0))
{
int length = (int)[textField.text length];
if (length==1)
{
if(range.length == 0)
{
[self.textField3 becomeFirstResponder];
return NO;
}

if([self.textField2.text length]==1)
{
self.textField2.text=@"";
[self.textFied1 becomeFirstResponder];
return NO;
}
}
if (([[self.textField4 text] length] > 0)&& ([[self.textFied1 text] length] > 0)&& ([[self.textField3 text] length] > 0)&&([[self.textField2 text] length] > 0)){

[self connected1];//this step is not executing

}

}
else if (textField == self.textField3&&([[string componentsSeparatedByCharactersInSet:acceptedInput] count] > 0))
{
int length = (int)[self.textField3.text length];
if (length==1)
{
if(range.length == 0)
{
[self.textField4 becomeFirstResponder];
return NO;
}

if([self.textField3.text length]==1)
{
self.textField3.text=@"";
[self.textField2 becomeFirstResponder];
return NO;
}
}
if (([[self.textField4 text] length] > 0)&& ([[self.textField2 text] length] > 0)&& ([[self.textFied1 text] length] > 0)&& ([[self.textField3 text] length] > 0)){
[self connected1];//this step is not executing
}
}
else if (textField == self.textField4&&([[string componentsSeparatedByCharactersInSet:acceptedInput] count] > 0))
{
int length = (int)[textField.text length];
if (length==1)

{

if(range.length == 0)
return NO;

if([self.textField4.text length]==1)
{
self.textField4.text=@"";
[self.textField3 becomeFirstResponder];

return NO;
}

}

// here my service code
if (([[self.textFied1 text] length] > 0)&& ([[self.textField2 text] length] > 0)&& ([[self.textField3 text] length] > 0)&&([[self.textField4 text] length] > 0))
{
[self connected1];//this step is not executing
}
}

return YES;
}

Answer

This is happening because in - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string you can't get string directly.

First make user's input text as:

NSString *content = [textField.text stringByAppendingString:string];

And then match every condition with content.

For Reference