UITextField键盘返回按钮应更改为完成按钮

时间:2012-09-19 04:34:56

标签: iphone ios ipad uitextfield ipod

我有五个textFields,比如textField1,textField2,textField3,textField4,textField5。

我想要做的是当所有textField包含一些文本时,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
{
}

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

3 个答案:

答案 0 :(得分:5)

你最好使用textFieldShouldBeginEditing,这里只检查所有textfields是否包含非空值,

所以代码看起来应该是这样的

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

答案 1 :(得分:1)

试试这个

if([your_textField.text length]>0)

your_textField.returnKeyType = UIReturnKeyDone;

答案 2 :(得分: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];
}

请记住space计为character,因此,如果您不想计算spaces,请先将其从textField.text中删除,然后进行比较。