检查将触发哪个 UITextField (textFieldShouldBeginEditing)

发布于 2024-10-22 07:08:30 字数 1505 浏览 8 评论 0原文

我一直在尝试实现将触发 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;
}

I have been trying to implement the checks for which UITextField will be triggered.
Here are the results of my findings for first two text fields only.
This gives me two errors that says "sender undeclared"...
Where am I doing wrong?
Thanks in advance
properties and synthesize are OK!
val is the tag value of the buttons for a caLculator.(such 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感情旳空白 2024-10-29 07:08:30

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

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

@interface XXXXX : XXXXXX <UITextFieldDelegate>

然后在您的实现中,使用

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

如果您使用不同的标签设置 textField 实例,如上面所示,只需使用 switch 语句来查找找出哪个文本字段正在调用该方法:

switch (textField.tag)
{
    case tagValue1:
    // Implement your logic here
    break;
    case tagValue2:
    // Implement your logic here
    break;
    ...
}

You haven't really explained what your issue is but just by looking at your code, you are using the incorrect delegate method names. You don't need a separate textFieldShouldBeginEditing: for each of your UITextField instances.

In your view controller class interface file, make sure you are declaring you conform to UITextFieldDelegate methods with:

@interface XXXXX : XXXXXX <UITextFieldDelegate>

Then in your implementation, use

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

And if you setup your textField instances with different tags as you seem to indicate above, simply use a switch statement to find out which textField is calling the method:

switch (textField.tag)
{
    case tagValue1:
    // Implement your logic here
    break;
    case tagValue2:
    // Implement your logic here
    break;
    ...
}
凝望流年 2024-10-29 07:08:30

正确遵守 UITextFieldDelegate 协议。然后两个 UITextField 将调用 textFieldShouldBeginEditing。 UITextfield 依赖于委托模式,因此它期望其委托实现某个方法,并且该方法必须正确命名。您尝试使用的是 UIButtons 使用的目标操作模式。

要找出调用了哪一个,您可以使用传递给该方法的 UITextfield 参数。不要忘记设置委托。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文