My UIButton
is set to disappear every time my UITextField
is done editing, I invoke the textFieldDidEndEditing:
method, and just have the button fade away. This works fine, unless I switch to another textfield without clicking out of the first one. So for instance, I'm on textfield A, and just tap textfield B, the keyboard still stays up, and so does the button. I don't believe that there is a method that covers textfields switching like this, only when ALL the textfields are done editing. Am I wrong? Here is my code:
-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{
negButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
negButton.frame = CGRectMake(textField.frame.origin.x, textField.frame.origin.y, 37, textField.frame.size.height);
[negButton setAlpha:0];
return YES;
}
-(void) textFieldDidBeginEditing:(UITextField *)textField
{
if ([textField isEditing])
{
[UIView animateWithDuration:0.3 animations:^
{
field = textField;
CGRect frame = textField.frame;
frame.size.width -= 40;
frame.origin.x += 40;
[negButton setAlpha:1];
[textField setFrame:frame];
[self.view addSubview:negButton];
}];
}
}
-(void) textFieldDidEndEditing:(UITextField *)textField
{
[UIView animateWithDuration:0.3 animations:^
{
CGRect frame = textField.frame;
frame.size.width += 40;
frame.origin.x -= 40;
[negButton setAlpha:0];
[textField setFrame:frame];
}
];
}
textFieldDidEndEditing:
method is called even when switching from text field to text field (it is used when the text field resigns its first responder status). If you want more help, please post the code that isn't working - lnafziger 2012-04-04 23:14
The problem here is the order that your delegate methods are being called.
Let's say that you are going from textField1 to textField2.
Once textField1 is already active and you click on textField2, they get called like this:
textFieldShouldBeginEditing (textField2)
textFieldShouldEndEditing (textField1)
textFieldDidEndEditing (textField1)
textFieldDidBeginEditing (textField2)
You are creating your new negButton
in textFieldShouldBeginEditing
which over-writes the reference to the "old" button (beside textField1) by creating one beside textField2 and storing it's reference instead. Next, you call textFieldDidEndEditing
and textFieldDidBeginEditing
on the new button.
What you want to do is move your code that is currently in textFieldShouldBeginEditing
to the beginning of textFieldDidBeginEditing
so that the previous two methods act upon the appropriate button before the new one is created.
It sounds to me like you are invoking the button to appear on the
textFieldShouldBeginEditing
method, which is fine, and you are removing it on the
textFieldDidEndEditing
method, also fine. Why you are not seeing the button disappear when you switch to another text box is because when you tap that text box, the shouldBeginEditing method is called immediately after the endEditing method, resulting in the button reappearing immediately after it is deleted.
This is the way it is supposed to work, and if you want it to work a different way, you will have to make the code specific to each text field
EX:
- (BOOL)textFieldShouldBeginEditing:(UITextField*)textField
{
if(textField == myField1)
{
//make button appear
}
else if(textField == myField2)
{
//Something else
}
}
Voila!
textFieldShouldBeginEditing:
that is called on the newly selected textfield to know that the user switched textfield - Paul.s 2012-04-04 23:03