Implementing the 'next' button in the keyboard

Discussion in 'iOS Programming' started by keeweepoo, Jan 2, 2010.

  1. keeweepoo macrumors newbie

    Joined:
    Dec 26, 2007
    #1
    I am having an awful hard time trying to implement the task of when the user selects the 'next' button in the keyboard, the user is then sent to the next text field to start editing. In my example I have three text fields. Here is what I've done, hopefully you can fill me in on where I am going wrong. Keep in mind I just picked up SDK a few weeks ago so that may be part of the problem :)

    In my ViewControler.h file I have created the following method

    Code:
    -(IBAction)nextPressed:(id) sender;
    In my ViewController.m file, I have created the following action

    Code:
    -(IBAction)nextPressed:(id) sender 
    {
    	if ([txtUserName isFirstResponder]) 
    	{
    		[txtUserName2 becomeFirstResponder];
    	}
    	if ([txtUserName2 isFirstResponder]) 
    	{
    		[txtUserName3 becomeFirstResponder];
    	}
    }
    In my .xib file, I have linked the first text field (right clicking on the text field and dragging to Files Owner and selecting the 'nextPressed:' option under Events) to my File Owner. I have tried linking to just the first text field and when that didn't work all of the three text fields. I've also tried linking not to the File's Owner but First Responder for one text field, then all of the text fields, with no luck. Also, for each text field I have selected the Return Key as 'NEXT'.

    Now when I Build/Run I am able to edit the text field and see the 'next' button in the lower right, however it doesn't move me to the next field.

    What step am I doing wrong here? I used instructions from this post (http://stackoverflow.com/questions/467881/how-do-you-change-the-uicontrol-with-focus-on-iphone) but seem to be missing something huge here.

    Any help with this would be *greatly* appreciated. I've been staring at this for the past four hours and Googling every possible search term I can come up with and can't easily wrap my head around the steps needed to accomplish this. Again, and help would be very helpful :)
     
  2. KoolStar macrumors demi-god

    KoolStar

    Joined:
    Oct 16, 2006
    Location:
    Kentucky
    #2
    Try using the tag id for the text field. You should be able to reasign first responder based on tag ID.
     
  3. miep macrumors newbie

    Joined:
    Jul 6, 2007
    #3
    May have a bit of a brain freeze, but it seems to me that the above code is a bit buggy.

    It's of the form: if (x == A) then make x B; if (x == B) then make x C. Obviously, after the first conditional, [txtUserName2] becomes the first responder - and then you hit the next conditional, which always evaluates to true, since you just MADE txtUserName2 the first responder.

    Could it be that you have a third if() that tests whether txtUserName3 is the first responder, and if so, makes txtUserName first responder again?

    If you do, you're actually executing what you coded - but it's not what you want. You're executing three if-statements each time, and the second and third always hold true when the first one does: if (a) then (b). ("Yup, B now holds"). if (b) then (c) ("Yup, c now holds"). if (c) then (a) ("yup, a now holds again").

    Throw in some else statements, to ensure that the second if() never gets executed if the first if() holds true.

    Code:
    -(IBAction)nextPressed:(id) sender 
    {
    	if ([txtUserName isFirstResponder]) {
    		[txtUserName2 becomeFirstResponder];
    	} else if ([txtUserName2 isFirstResponder]) {
    		[txtUserName3 becomeFirstResponder];
    	} else if () {}
    }
     

Share This Page