Wednesday, January 12, 2011

How to handle multiple textFileds

In Load View,

   for(int i=0;i<8;i++)
        {
            UITextField* Field = [[UITextField alloc] initWithFrame:CGRectMake(150,44+35+40*i,160,30)];
            Field.borderStyle = UITextBorderStyleRoundedRect;
            Field.delegate = self;
            Field.tag = i+1;
            [self.view addSubview:Field];
            [mTextFiledsArray addObject:Field];
            [Field release];
        }

Use these Delegate methods,

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    if(textField.tag == 5) self.view.frame = CGRectMake(0,-20,320,480);
    else if(textField.tag == 7) self.view.frame = CGRectMake(0,-95,320,480);       
    else if(textField.tag == 8) self.view.frame = CGRectMake(0,-130,320,480);       
    [UIView commitAnimations];
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    for(int i=0;i<[mTextFiledsArray count];i++)
    {
        UITextField* tf = [mTextFiledsArray objectAtIndex:i];
        if(textField == tf)
        {
            if(i+1 < [mTextFiledsArray count])
                [[mTextFiledsArray objectAtIndex:i+1] becomeFirstResponder];
            else    
            {
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:0.3];
                self.view.frame = CGRectMake(0,0,320,480);       
                [UIView commitAnimations];
                [textField resignFirstResponder];
            }
        }
    }
    return YES;
}

2 comments:

  1. copying and pasting of coding does nt make understand by Begineer...

    PLz make some Discription before line of code...for effective understand..Who started it as thier first programing language..to learn.

    U r stuff is very useful...kepp going on..

    Thank U.

    ReplyDelete
  2. Thank you nagaraju for your valuable feedback.
    Right now I added a little description like where to paste.

    I will elaborate the description for effective understanding.

    ReplyDelete