简体   繁体   English

UITextfield成为第一响应者不起作用

[英]UITextfield become first responder not working

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. 当我按下next按钮时,下一个文本字段应成为第一响应者,但这不会发生。 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. 我进入日志“姓”,但光标未显示,如果我键入某些内容,它将不会出现在任何地方。

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. 您的问题是,这些if语句中的每一个都是正确的,因为成为第一响应者是直接的事情。

In other words, you should be using else if (...) statements instead of discrete if statements. 换句话说,您应该使用else if (...)语句,而不是离散的if语句。

create an array of textfield object lets say textFieldArray. 创建一个textfield对象数组,可以说textFieldArray。 then on prev and next button tab call following func 然后在上一个和下一个按钮选项卡上调用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;
    }
}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM