检查将触发哪个 UITextField (textFieldShouldBeginEditing)
我一直在尝试实现将触发 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您还没有真正解释您的问题是什么,但仅通过查看您的代码,您使用了不正确的委托方法名称。您不需要为每个 UITextField 实例使用单独的 textFieldShouldBeginEditing: 。
在您的视图控制器类接口文件中,确保您声明符合 UITextFieldDelegate 方法:
然后在您的实现中,使用
如果您使用不同的标签设置 textField 实例,如上面所示,只需使用 switch 语句来查找找出哪个文本字段正在调用该方法:
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:
Then in your implementation, use
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:
正确遵守 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.