2

当我专注于 UITextField 控件时,我将按钮添加为子视图。当我从 UITextField 中移除焦点时,我移除了添加按钮。这在 UITextField 中有一些文本时有效。但是当没有文本时,按钮不会消失。

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.

这是我添加和删除取消按钮作为子视图的代码

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

这是需要消失的空 UITextField 中的添加按钮。

在此处输入图像描述

4

3 回答 3

1

在此块中执行按钮删除操作..

- (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;
}
于 2013-01-21T04:02:25.227 回答
1

您的代码工作正常。但问题在于按钮和textFieldDidEndEditing:方法的索引

首先

如果文本字段中有占位符文本和文本,则按钮的索引为 2

接下来该方法应该像这样实现

- (void)textFieldDidEndEditing:(UITextField *)textField

最后为您的代码添加一些优化,工作代码如下所示

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

这应该与 dotnet 中的文本框的 Focus() 方法完全相同:)无需填充 textField 以隐藏按钮。希望对您有所帮助...

于 2013-01-21T06:45:04.250 回答
0

这是我的解决方案:

(在 UITextField 的子类中)

- (UITextFieldViewMode)rightViewMode
{
    return [self.text isEqualToString:@""] ? UITextFieldViewModeNever : UITextFieldViewModeAlways;
}
于 2015-03-11T17:27:57.100 回答