Local declaration of 'fieldPhone' hides instance variable - ios

I keep getting the warning that I posted in the title with these methods...I put self. in front of them but it then messed up my menus. I saw some similar posts, but nothing to the point and absolute. Any help on this is appreciated.
- (BOOL)textField:(UITextField *)fieldPhone shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString* totalString = [NSString stringWithFormat:#"%#%#",self.fieldPhone.text,string];
// if it's the phone number textfield format it.
if(fieldPhone.tag==102 ) {
if (range.length == 1) {
// Delete button was hit.. so tell the method to delete the last char.
fieldPhone.text = [self formatPhoneNumber:totalString deleteLastChar:YES];
} else {
fieldPhone.text = [self formatPhoneNumber:totalString deleteLastChar:NO ];
}
return false;
}
return YES;
}

Change the function textField name to a generic name, problem is that you named it equal to your class variable, change it from:
- (BOOL)textField:(UITextField *)fieldPhone shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
to:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
You can check it inside the function if needed:
if (textField == self.phoneField) {
}

Related

using rangeOfString and textfield shouldChangeCharactersInRange - the delete character?

I'm using rangeofString and textfield: shouldChangeCharactersinRange: to restrict the types of keystrokes that will be valid in a textfield.
-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSString *includeString = #"1234567890-()+" ;
if ([includeString rangeOfString:string].location == NSNotFound) {
return NO;
}
return YES;
}
this works fine EXCEPT i now can't use the delete key. Any ideas how to represent the delete key to add it to the includeString?
I tried
`NSString *includeString = #"1234567890-()+\b"
but that didn't work - neither did it allow the \ or b characters to appear which i thought odd
Thanks
The replacement string string is empty when characters are deleted.
Since rangeOfString:string returns NSNotFound for an empty string,
you have to check for that situation first:
-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if ([string length] == 0)
return YES;
NSString *includeString = #"1234567890-()+" ;
if ([includeString rangeOfString:string].location == NSNotFound) {
return NO;
}
return YES;
}
Update: As #rmaddy correctly pointed out, the above method fails if more than one
character is pasted into the text field. The following method checks if all
characters of the replacement string are valid. (There are probably many solutions,
this is only one of them.)
-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
static NSString *includeString = #"1234567890-()+";
NSCharacterSet *includeSet = [NSCharacterSet characterSetWithCharactersInString:includeString];
if ([[string stringByTrimmingCharactersInSet:includeSet] length] > 0)
return NO;
return YES;
}
Note that the empty string does not need to be handled separately anymore.

Calling a method on every edit of UITextField

I'm now programming a simple app that has 3 UITextFields and if I edit one, the other two should scale together with it.
I tried using
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
this method, but:
replacement string is the last character that was typed in
can't figure out how backspace works there
it is being called a little too early
if I can "fix" the first point(by sending
[NSString stringWithFormat:#"%#%#", [textField text], string];
as a parameter), it will not "fix" the second point, because string variable is:
(lldb) po string
(NSString *) $1 = 0x0080cf14 <object returned empty description>
So the question is: is there any method that is being called AFTER textFieldShouldChangeCharactersInRange:? Or is there a way to:
return YES in textFieldShouldChangeCharactersInRange: method
and THEN call a method to change the values of the 2 other UITextFields?
EDIT
I could use the following:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
[self performSelector:#selector(myMethod:) withObject:textField afterDelay:0.1];
return YES;
}
but it doesn't seem to be the safest solution
The backspace works modifying the NSRange with a empty string. What you can do is modify the three text field in the textField:shouldChange and then return NO to the method.
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
textField.text = [textField.text stringByReplacingCharactersInRange:range withString:string]; // this is what will happen if you return yes to this method
anotherTextField.text = // do whatever u need
yetAnotherTextField.text = // do whatever u need
return NO;
}
The following code prints correctly the full text of the text view (with the last character that was inserted), can that be useful?
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if ([string length])
NSLog(#"Current Text:%#", [textField.text stringByAppendingString:string]);
else
NSLog(#"Current Text:%#", [textField.text substringWithRange:(NSRange){0, [textField.text length]-1}]);
return YES;
}
The backspace just sends an empty string in the string parameter.
For textFields:
[yourtextField addTarget:self action:#selector(textFieldDidChange) forControlEvents:UIControlEventEditingChanged];
-(void)textFieldDidChange
{
//Your Code
}

Is there anyway to limit the number of character in UITextfield to 5?

Iam trying to limit the number of character in a textfield to 5 .So when we trying to enter the 6th character it will not do anything? is that possible .I saw below code but its not working .I have searched this in google got some results like
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return (newLength > 5) ? NO : YES;
}
its not working am still able to add more than 5 character
Please check your delegate connection with UITextfield
Is the delegate being called? If YES, then just try this:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([textField.text length] > 5) {
textField.text = [textField.text substringToIndex:5-1];
return NO;
}
return YES;
}

Using the same text field for URL and Google Search in iOS

I am trying to get the URL and google search in the same Text field.. the method I am using works alright but probably there is a better way. What I am doing is to check if there is a dot in the input like www.google.com, if a dot is not found, then search it on google..
NSRange range = [textField.text rangeOfString:#"."];
textField.text = (range.location != NSNotFound) ?
[NSString stringWithFormat:#"%#", textField.text] :
[#"http://www.google.com/search?q=" stringByAppendingString:textField.text ];
If the input has a dot, then search fails.. Is there a better way to do it? Thanks..
U can prevent user from entering . dot in UITextField like this by using UITextField's delegate method:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([string isEqualToString:#"."])
{
return NO;
}
else
{
return YES;
}
}

shouldChangeCharactersInRange logic

I can't seem to figure out the best way to handle this. When user presses decimal point I want it to move to next textfield. The problem I'm having is it's displaying the decimal in the next text field and only works if I type a decimal only. I believe I understand why it's only allowing decimal is because of the way i'm starting if statement, but I can't seem to figure out the best approach here.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *decmial = #".";
if ([textField.text isEqualToString:#"."])
{
if (octet1.text == decmial) {
octet1.text = [octet1.text stringByReplacingOccurrencesOfString:decmial withString:#""];
[octet1 isFirstResponder];
[octet2 becomeFirstResponder];
}
else if (octet2.text == decmial) {
octet2.text = [octet2.text stringByReplacingOccurrencesOfString:decmial withString:#""];
[octet2 isFirstResponder];
[octet3 becomeFirstResponder];
}
else if (octet3.text == decmial) {
octet3.text = [octet3.text stringByReplacingOccurrencesOfString:decmial withString:#""];
[octet3 isFirstResponder];
[octet4 becomeFirstResponder];
}
else if (octet4.text == decmial) {
octet4.text = [octet4.text stringByReplacingOccurrencesOfString:decmial withString:#""];
[octet4 isFirstResponder];
[myBitMask becomeFirstResponder];
}
}
return YES;
}
Currently, you're only doing anything if the entire content of the text field being modified is exactly ".". I assume that you should really be testing to see if the user has typed that in instead, by comparing with the replacement string:
if ([string isEqualToString:#"."]) {
You then go on to test each of your text fields in turn, without any regard for the actual text field being modified (the one passed into the function). You try to check each one to see if it's exactly "." (except you don't, because you're using == instead of isEqualToString), and if it is then you replace that entire string by what ever the user's just typed. Very odd indeed!
Based on your description of what yo want to do, how about:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *decmial = #".";
if ([string isEqualToString:decmial])
{
textField.text = [textField.text stringByReplacingOccurrencesOfString:decmial withString:#""];
if (textField == octet1) {
[octet2 becomeFirstResponder];
}
else if (textField == octet2) {
[octet3 becomeFirstResponder];
}
else if (textField == octet3) {
[octet4 becomeFirstResponder];
}
else if (textField == octet4) {
[myBitMask becomeFirstResponder];
}
// We've done the replacement ourselves and moved to the next field. Don't allow the system to accept the change
return NO;
}
// Default behavior
return YES;
}

Resources