UITextfield 成为第一响应者不工作

发布于 2024-11-05 22:23:33 字数 1155 浏览 0 评论 0原文

我的键盘顶部有一个工具栏,带有上一个和下一个按钮。当我按下下一个按钮时,下一个文本字段应该成为第一响应者,但这种情况不会发生。这是我的代码。

-(IBAction) nextfield {
    if([name isFirstResponder] == TRUE) {
        [name resignFirstResponder];
        [surname becomeFirstResponder];
        if(surname.editing == TRUE)
            NSLog(@"surname");
    }
    if([surname isFirstResponder] == TRUE) {
        [email becomeFirstResponder];
    }
    if([email isFirstResponder] == TRUE) {
        [password becomeFirstResponder];
    }
    if([password isFirstResponder] == TRUE) {
        [confirm becomeFirstResponder];
    }
    if([confirm isFirstResponder] == TRUE) {
        [country becomeFirstResponder];
    }
    if([country isFirstResponder] == TRUE) {
        if(country.text == @"United States") {
            [state setEnabled:TRUE];
            [state becomeFirstResponder];
        }
        else {
            [type becomeFirstResponder];
        }
    }
    if([state isFirstResponder] == TRUE) {
        [type becomeFirstResponder];
    }
    if ([type isFirstResponder] == TRUE) {
        [name becomeFirstResponder];
    }
}

我进入“姓氏”日志,但光标不显示,如果我输入某些内容,它不会出现在任何地方。

I have a toolbar on top of the keyboard with a prev and next button. When I press the next button the next textfield should become first responder but this doesn't happen. Here is my code.

-(IBAction) nextfield {
    if([name isFirstResponder] == TRUE) {
        [name resignFirstResponder];
        [surname becomeFirstResponder];
        if(surname.editing == TRUE)
            NSLog(@"surname");
    }
    if([surname isFirstResponder] == TRUE) {
        [email becomeFirstResponder];
    }
    if([email isFirstResponder] == TRUE) {
        [password becomeFirstResponder];
    }
    if([password isFirstResponder] == TRUE) {
        [confirm becomeFirstResponder];
    }
    if([confirm isFirstResponder] == TRUE) {
        [country becomeFirstResponder];
    }
    if([country isFirstResponder] == TRUE) {
        if(country.text == @"United States") {
            [state setEnabled:TRUE];
            [state becomeFirstResponder];
        }
        else {
            [type becomeFirstResponder];
        }
    }
    if([state isFirstResponder] == TRUE) {
        [type becomeFirstResponder];
    }
    if ([type isFirstResponder] == TRUE) {
        [name becomeFirstResponder];
    }
}

I get in Log "surname" but the cursor doesn't show up and if i type something it doesn't appear anywhere.

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

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

发布评论

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

评论(2

乱了心跳 2024-11-12 22:23:33

因此,您要检查 name 是否是第一响应者,如果是,则将 surname 设置为第一响应者。

然后您检查姓氏是否是第一响应者,如果是,则将电子邮件设置为第一响应者。

然后你检查电子邮件是否是第一响应者,如果是……

你的问题是这些 if 语句中的每一个都是正确的,因为成为第一响应者是立即发生的事情。

换句话说,您应该使用 else if (...) 语句而不是离散的 if 语句。

So you're checking to see if name is the first responder, and if it is, then making surname the first responder.

Then you check and see if surname is the first responder, and if it is, you make email the first responder.

Then you check and see if email is the first responder, and if it is....

Your problem is that every one of those if statements is true, because becoming first responder is an immediate thing.

In other words, you should be using else if (...) statements instead of discrete if statements.

你是暖光i 2024-11-12 22:23:33

创建一个文本字段对象数组,比如 textFieldArray。然后在上一个和下一个按钮选项卡上调用以下函数

 -(void)gotoPrevTextfield

{
  for(int i=1 ;i<[textFieldArray count];i++)
{
    if([textFieldsArray [i] isFirstResponder])
    {
        [textFieldsArray[i-1] becomeFirstResponder];
        [textFieldsArray [i] resignFirstResponder];

         break;
    }
}}

    -(void)gotoNextTextfield
   {
for( int i=0 ;i<[textFieldArray count];i++)
{
    if([textFieldsArray [i] isFirstResponder])
    {
        [textFieldsArray [i+1] becomeFirstResponder];
        [textFieldsArray [i] resignFirstResponder];

        break;
    }
}}

create an array of textfield object lets say textFieldArray. then on prev and next button tab call following func

 -(void)gotoPrevTextfield

{
  for(int i=1 ;i<[textFieldArray count];i++)
{
    if([textFieldsArray [i] isFirstResponder])
    {
        [textFieldsArray[i-1] becomeFirstResponder];
        [textFieldsArray [i] resignFirstResponder];

         break;
    }
}}

    -(void)gotoNextTextfield
   {
for( int i=0 ;i<[textFieldArray count];i++)
{
    if([textFieldsArray [i] isFirstResponder])
    {
        [textFieldsArray [i+1] becomeFirstResponder];
        [textFieldsArray [i] resignFirstResponder];

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