user198725878
user198725878

Reputation: 6386

Help me on UITextField

i have list of UITextFields.

i want to move to the next text fields one by one after entering input in it.

since the keyboard is hiding the fields....

can you people show me that how can i accomplish that...

Thank a lot for your time.

Upvotes: 0

Views: 174

Answers (2)

Christian Loncle
Christian Loncle

Reputation: 1584

An alternative approach could be to use a UITableView. Then most of the controls are ready. But you can also make it all happen by yourself.

First put your UITextfields on a separate UIView. Make that UIView an outlet en link it properly. That way you can easily move the textfields up with an animation.

Then do something like this in the delegate methods:

//  Sets the label of the keyboard's return key to 'Done' when the insertion
//  point moves to the table view's last field.
//
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
 {
     if ([textField tag] == myLastUITextField)
 {
    [textField setReturnKeyType:UIReturnKeyDone];
 }  
   return YES;
 }

- (BOOL)textFieldShouldReturn:(UITextField *)textField
 {
   if ([textField returnKeyType] != UIReturnKeyDone)
 {
    //  If this is not the last field (in which case the keyboard's
    //  return key label will currently be 'Next' rather than 'Done'), 
    //  just move the insertion point to the next field.
    //
    NSInteger nextTag = [textField tag] + 1;

    UIView *nextTextField = [myTextView viewWithTag:nextTag];

    // here starts your animation code
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3]; 
    CGRect rect = textFieldView.frame;
    rect.origin.y = -44;
    textFieldView.frame = rect;
    [UIView commitAnimations];        
    // here ends your animation code. Play with it
    [nextTextField becomeFirstResponder];
  }
else 
{
    //  do what you want to do with the last UITextfield  
}
    return YES;
}

`

I think it will not immediately work by copy-pasting but I hope it points in a direction. Good luck

Upvotes: 1

Swastik
Swastik

Reputation: 2425

  • (void)shiftViewUp:(BOOL)up {

[UIView beginAnimations:nil context:NULL];

[UIView setAnimationDuration:0.3];

CGRect rect = self.view.frame;
if (movedUp)
{

    if(rect.origin.y == 0){
        rect.origin.y -= 85.0;
        rect.size.height += 85.0;
    }
}
else
{

    if(rect.origin.y != 0.0){
        rect.origin.y += 85.0;
        rect.size.height -= 85.0;
    }
}

self.view.frame = rect;

[UIView commitAnimations];

}

Basically you can call this method with BOOL YES when you begin editing the textfiled & with BOOL no when you end editing. You just need to track which textfield is editing based on that you can adjust shifting (eg 85 here)

Upvotes: 2

Related Questions