1

我一直在尝试实现将触发 UITextField 的检查。以下是我仅针对前两个文本字段的发现结果。这给了我两个错误,上面写着“未声明发件人”......我在哪里做错了?提前感谢 属性和合成都OK!val 是计算器按钮的标记值。(例如 0,1,2,3,4,5,6,7,8,9)

//.h file

IBOutlet UITextField *textFieldLoanAmountDisplay;
IBOutlet UITextField *textFieldInitDepositDisplay;


// .m file

const NSString *textField1Code= @"1";
const NSString *textField2Code= @"2";


-(BOOL)textField1ShouldBeginEditing:(UITextField *)textFieldLoanAmountDisplay {
    if (textFieldLoanAmountDisplay == textField1Code) 
    {
        UIButton *buttonPressed = (UIButton *)sender;
        int val = buttonPressed.tag;
        if ( [textFieldLoanAmountDisplay.text compare:@"0"] == 0 ) {
            textFieldLoanAmountDisplay.text = [NSString stringWithFormat:@"%d", val ];
        } else {
            textFieldLoanAmountDisplay.text = [NSString stringWithFormat:@"%@%d", textFieldLoanAmountDisplay.text, val ];
        }
    }
    return NO;    
}

-(BOOL)textField2ShouldBeginEditing:(UITextField *)textFieldInitDepositDisplay {
    if (textFieldInitDepositDisplay == textField2Code) 
    {
        UIButton *buttonPressed = (UIButton *)sender;
        int val = buttonPressed.tag;
        if ( [textFieldInitDepositDisplay.text compare:@"0"] == 0 ) {
            textFieldInitDepositDisplay.text = [NSString stringWithFormat:@"%d", val ];
        } else {
            textFieldInitDepositDisplay.text = [NSString stringWithFormat:@"%@%d", textFieldInitDepositDisplay.text, val ];
        }
    }
    return NO;
}
4

2 回答 2

2

您还没有真正解释您的问题是什么,但只是通过查看您的代码,您使用了不正确的委托方法名称。对于每个 UITextField 实例,您不需要单独的 textFieldShouldBeginEditing:。

在您的视图控制器类接口文件中,确保您声明您符合 UITextFieldDelegate 方法:

@interface XXXXX : XXXXXX <UITextFieldDelegate>

然后在您的实现中,使用

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

如果您使用不同的标签设置您的 textField 实例,就像您在上面指出的那样,只需使用 switch 语句来找出哪个 textField 正在调用该方法:

switch (textField.tag)
{
    case tagValue1:
    // Implement your logic here
    break;
    case tagValue2:
    // Implement your logic here
    break;
    ...
}
于 2011-03-15T22:24:31.297 回答
0

Conform to the UITextFieldDelegate protocol correctly. textFieldShouldBeginEditing will be called then by both UITextFields. The UITextfield depends on the delegate pattern so it expects a certain method to be implemented by its delegate and that method has to named correctly. What you're trying to use is the target action pattern which is used by UIButtons for example.

To find out which one has been called you can use the UITextfield parameter which is passed to the method. Don't forget to set the delegate.

于 2011-03-15T22:13:30.713 回答