2

我有五个文本字段,例如 textField1、textField2、textField3、textField4、textField5。

我想要做的是UITextField当所有文本字段都包含一些文本时,键盘返回按钮应该更改为完成按钮。我试过了,

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if([textField1.text doubleValue]!=0)
        if([textField2.text doubleValue]!=0)
            if([textField3 doubleValue]!=0)
                if([textField4 doubleValue]!=0)
                    if([textField5 doubleValue]!=0)
                    {
                        [textField setReturnKeyType:UIReturnKeyDone];
                    }
                    else
                        [textField setReturnKeyType:UIReturnKeyDefault];
                    else
                        [textField setReturnKeyType:UIReturnKeyDefault];
                    else
                        [textField setReturnKeyType:UIReturnKeyDefault];
                    else
                        [textField setReturnKeyType:UIReturnKeyDefault];
                    else
                        [textField setReturnKeyType:UIReturnKeyDefault];
}

我也试过

- (void)textFieldDidEndEditing:(UITextField *)textField
{
}

两者都不起作用...请任何人帮助我...

4

3 回答 3

5

您最好使用 textFieldShouldBeginEditing,这里只需检查所有文本字段是否包含非空值,

所以代码应该是这样的

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if([[tf1.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] > 0 && [[tf2.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] > 0 [[tf3.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] > 0) {
        [textField setReturnKeyType:UIReturnKeyDone];
    }
    return TRUE;
}

////编辑

我检查了这段代码。它工作正常。所以试试这个,让我知道。

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if ([self areAllFieldsNonEmpty]) {
        [textField setReturnKeyType:UIReturnKeyDone];
        [textField reloadInputViews];
    } else {
        [textField setReturnKeyType:UIReturnKeyDefault];
        [textField reloadInputViews];
    }
    return TRUE;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if ([self areOtherFiledsNonEmptyThan:textField]) {
        if (range.location == 0 && range.length == 0) {
            [textField setReturnKeyType:UIReturnKeyDone];
            [textField reloadInputViews];
        } else if (range.location == 0 && range.length == 1) {
            [textField setReturnKeyType:UIReturnKeyDefault];
            [textField reloadInputViews];
        }
    }
    return TRUE;
}

- (NSInteger)lengthOf:(UITextField *)tf {
    return [[tf.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length];
}

- (BOOL)areOtherFiledsNonEmptyThan:(UITextField *)tf {
    BOOL isEmpty = TRUE;
    for (UITextField *textField in self.view.subviews) {
        if ([textField isKindOfClass:[UITextField class]] && tf != textField) {
            if ([self lengthOf:textField] == 0) {
                isEmpty = FALSE;
                break;
            }
        }
    }
    return isEmpty;
}

- (BOOL)areAllFieldsNonEmpty {
    BOOL isEmpty = TRUE;
    for (UITextField *textField in self.view.subviews) {
        if ([textField isKindOfClass:[UITextField class]]) {
            if ([self lengthOf:textField] == 0) {
                isEmpty = FALSE;
                break;
            }
        }
    }
    return isEmpty;
}
于 2012-09-19T04:52:09.813 回答
1

试试这个

if([your_textField.text length]>0)

your_textField.returnKeyType = UIReturnKeyDone;
于 2012-09-19T05:15:57.383 回答
0

您必须通过两种方法检查您的状况:

-(void)textFieldDidBeginEditing:(UITextField *)textField;
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;

因此,为代码的可重用性创建一个条件函数 -

-(void)checkCondition;    

然后在你的ViewController.m课堂上这样做 -

-(void)checkCondition
{
    if([txtField1.text length]>0 && [txtField2.text length]>0 && [txtField3.text length]>0 && [txtField4.text length]>0 && [txtField5.text length]>0)
    {
        txtField1.returnKeyType = UIReturnKeyDone;
        txtField2.returnKeyType = UIReturnKeyDone;
        txtField3.returnKeyType = UIReturnKeyDone;
        txtField4.returnKeyType = UIReturnKeyDone;
        txtField5.returnKeyType = UIReturnKeyDone;
    }

    else
    {
        txtField1.returnKeyType = UIReturnKeyDefault;
        txtField2.returnKeyType = UIReturnKeyDefault;
        txtField3.returnKeyType = UIReturnKeyDefault;
        txtField4.returnKeyType = UIReturnKeyDefault;
        txtField5.returnKeyType = UIReturnKeyDefault;
    }

}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
     [self checkCondition];
     [textField reloadInputViews];
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
{
     [self checkCondition];
     [textField reloadInputViews];
}

Remember space is count as a character, So if you dont want to count spaces then first remove them from textField.text and then compare.

于 2012-09-19T08:24:53.227 回答