如何在 tabBar 按钮的操作上从一个文本字段移动到下一个文本字段
在我的 iPhone 应用程序中,我有 6 个文本字段和一个 tabBar(有 3 个按钮“下一步”、“上一个”、“完成”)。
我需要做的就是当我单击下一个按钮时,焦点应该从第一个文本字段移动到下一个文本字段。
在“下一步”按钮的 IBAction 上,我编写了以下代码:
if ([txt1 isFirstResponder]==YES) {
NSLog(@"TXT1");
[txt1 resignFirstResponder];
[txt2 becomeFirstResponder];
}
else if ([txt2 isFirstResponder]==YES) {
[txt2 resignFirstResponder];
[txt3 becomeFirstResponder];
}
else if ([txt3 isFirstResponder]==YES) {
[txt3 resignFirstResponder];
[txt4 becomeFirstResponder];
}
else if ([txt4 isFirstResponder]==YES) {
[txt4 resignFirstResponder];
[txt5 becomeFirstResponder];
}
else if ([txt5 isFirstResponder]==YES) {
[txt5 resignFirstResponder];
[txt6 becomeFirstResponder];
}
else if ([txt6 isFirstResponder]==YES) {
[txt6 resignFirstResponder];
}
当我使用此代码时,NSLog 会打印无限次。
可以做什么?
In my iphone app I have 6 textFields, and a tabBar (having 3 buttons Next,Previous,Done).
All I need to do is when I click on next button, the focus should move from first textField to next textField.
On Next button's IBAction I write the following code :
if ([txt1 isFirstResponder]==YES) {
NSLog(@"TXT1");
[txt1 resignFirstResponder];
[txt2 becomeFirstResponder];
}
else if ([txt2 isFirstResponder]==YES) {
[txt2 resignFirstResponder];
[txt3 becomeFirstResponder];
}
else if ([txt3 isFirstResponder]==YES) {
[txt3 resignFirstResponder];
[txt4 becomeFirstResponder];
}
else if ([txt4 isFirstResponder]==YES) {
[txt4 resignFirstResponder];
[txt5 becomeFirstResponder];
}
else if ([txt5 isFirstResponder]==YES) {
[txt5 resignFirstResponder];
[txt6 becomeFirstResponder];
}
else if ([txt6 isFirstResponder]==YES) {
[txt6 resignFirstResponder];
}
when I use this code NSLog prints infinite times.
What can be done??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可能是因为如果
txt1 isFirstResponder
,您将txt2设置为firstResponder,然后检查txt2是否是第一响应者。您应该在每个if
语句的末尾放置一个break;
。Might be because if
txt1 isFirstResponder
, you're setting txt2 as firstResponder, then checking if txt2 is first responder. You should put abreak;
at the end of eachif
statement.哎呀,这是我的错误。我忘记将 textField 的委托连接到文件所有者..
谢谢你们。
Oops, its my mistake. I Forgot to connect textField's delegate to Files Owner..
Thank you guys.
实际上,在每个 if 语句中,您不需要
resignFirstResponder
,因为您不希望每次移动到另一个字段时都关闭键盘。相反,将唯一的
resignFirstResponder
保留在txt6
中Actually, in each if statement, you do not need the
resignFirstResponder
, since you do not want to dismiss the keyboard each time you move to another field.Instead, leave the only
resignFirstResponder
intxt6