Reputation: 14148
when I focus on a UITextField control, I add button as subview. when I remove the focus from UITextField, I remove the add button. This works when there is some text in the UITextField. But when there is no text, the button does not go away.
Q. How can I remove the UIButton from UITextField when the UITextField is empty.
I also want to be able to show *default* placeholder-text for UITextField when add button is removed.
Here is my code to add and remove unbutton as subview
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
if(textField == txtToEmailAddress)
{
txtToEmailAddress.rightView = button;
txtToEmailAddress.rightViewMode = UITextFieldViewModeAlways;
[button addTarget:self action:@selector(AddEmailAddress:) forControlEvents:UIControlEventTouchUpInside];
[self.txtToEmailAddress addSubview:button];
}
if(textField == txtCallPhoneNum)
{
txtCallPhoneNum.rightView = button;
txtCallPhoneNum.rightViewMode = UITextFieldViewModeAlways;
[button addTarget:self action:@selector(AddPhoneNumber:) forControlEvents:UIControlEventTouchUpInside];
[self.txtCallPhoneNum addSubview:button];
}
if(textField == txtTextNumbers)
{
txtTextNumbers.rightView = button;
txtTextNumbers.rightViewMode = UITextFieldViewModeAlways;
[button addTarget:self action:@selector(AddTextNumber:) forControlEvents:UIControlEventTouchUpInside];
[self.txtTextNumbers addSubview:button];
}
return YES;
}
- (BOOL)textFieldDidEndEditing:(UITextField *)textField
{
UIButton *button = nil;
if(textField == txtToEmailAddress)
{
button = (UIButton *)[txtToEmailAddress.subviews objectAtIndex:1];
}
if(textField == txtCallPhoneNum)
{
button = (UIButton *)[txtCallPhoneNum.subviews objectAtIndex:1];
}
if(textField == txtTextNumbers)
{
button = (UIButton *)[txtTextNumbers.subviews objectAtIndex:1];
}
if (button != nil)
{
button.hidden = YES;
[button removeFromSuperview];
}
return YES;
}
Here is the add button in the empty UITextField that needs to go away.
Upvotes: 2
Views: 344
Reputation: 2564
Here is my solution:
(in a subclass of UITextField)
- (UITextFieldViewMode)rightViewMode
{
return [self.text isEqualToString:@""] ? UITextFieldViewModeNever : UITextFieldViewModeAlways;
}
Upvotes: 0
Reputation: 2343
Your code works fine.But problem is with the index of button and textFieldDidEndEditing:
method
Firstly
The index of the button is 2 if you have a placeholder text and text in textfield
Next the method should be implemented like this
- (void)textFieldDidEndEditing:(UITextField *)textField
And the finally adding some optimization to your code, the working code looks like this
-(void)textFieldDidBeginEditing:(UITextField *)textField{
UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
textField.rightView = button;
textField.rightViewMode = UITextFieldViewModeAlways;
[button addTarget:self action:@selector(AddTextNumber:) forControlEvents:UIControlEventTouchUpInside];
[textField addSubview:button];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
UIButton *button = nil;
NSLog(@"textField>>%@",[textField.subviews description]);
if([textField.text isEqualToString:@""])
button = (UIButton *)[textField.subviews objectAtIndex:2];
else
button = (UIButton *)[textField.subviews objectAtIndex:1];
if (button != nil)
{
button.hidden = YES;
[button removeFromSuperview];
}
}
This should work perfectly same as your textbox's Focus() method in dotnet :) No need to populate textField for hiding button .Hope it helps...
Upvotes: 1
Reputation: 5955
Do the button remove action in this block..
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
if(newString.length==0) {
button.hidden=YES;
textField.text=@"Default text";
}
return YES;
}
Upvotes: 1