Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

next UITextfeild must get focus.

I am generating 2 sets of textfeilds(Frame1 and Frame 2) dynamically lets say 1st Frame1 include 4 textfeilds and Frame2 includes 4 textfeilds now what i need to do is if i entered text in 1st textfeild and hit next focus must be transferred to Frame 2's first textfeild and again if i am done with editing this textfeild and hit next focus must be transferred to Frame1's textfeild number 2
please help me out i am new in iphone development.
Thanks in Advance..

what i tried is
UIResponder * txt1Responder = [txtFldFrame1.superview viewWithTag:x];
if(txt1Responder)
[txt1Responder becomeFirstResponder];
else
[txt1Responder resignFirstResponder];

Replies

  • mariano_donatimariano_donati Posts: 345Registered Users @ @
    I think tags should be used to identify views with an unique identifier, but not an index. If you want your views to be accessed by index, you can declare an IBOutletCollection.

    I've implemented it in the past like this:

    In your view controller, implement this method and call it from your viewDidLoad.
    The textFields collection is an IBOutletCollection to which you add each of your UITextField in Interface Builder.
    
    - (void)setupTextFields
    {
        for (UITextField *textField in self.textFields)
        {
            textField.delegate = self;
            textField.returnKeyType = UIReturnKeyNext;
        }
        
        UITextField *lastTextField = [self.textFields lastObject];
        lastTextField.returnKeyType = UIReturnKeyDone;
    }
    
    Your view controller must implement the UITextFieldDelegate as follows:
    
    - (void)textFieldDidBeginEditing:(UITextField *)textField
    {
        self.selectedTextField = textField;
        [self focusScrollViewOnTextField:textField];
    }
    
    - (BOOL)textFieldShouldReturn:(UITextField *)textField
    {
        if ([self.textFields lastObject] != self.selectedTextField)
        {
            [self focusOnNextTextField];
        }
        else
        {
            [self onSubmit:textField];
        }
        
        return NO;
    }
    
    And finally the method to focus on the next field:
    
    - (void)focusOnNextTextField
    {
        NSInteger indexOfNextTextField = [self.textFields indexOfObject:self.selectedTextField] + 1;
        UITextField *nextTextField = [self.textFields objectAtIndex:indexOfNextTextField];
        [nextTextField becomeFirstResponder];
    }
    
    It's better to declare your onSubmit method as an IBAction so you can link it to the submit button in Interface Builder, thus assuring you only have one entry point for form submission.
    Also, depending on how many textfields you have to display you might end up needing a container scroll view to avoid the keyboard to cover your textfields while editing.

    Hope that helps.

    <a href="http://www.thisissudoku.com"; target="_blank">This Is Sudoku, a logic-based sudoku game</a>
  • chaudhariabhijitchaudhariabhijit Posts: 23New Users @
    but my textfeilds are dynamic how can i create IBOutletCollection of those?
  • mariano_donatimariano_donati Posts: 345Registered Users @ @
    No need to create a IBOutletCollection in that case. Create a mutable array and add textfields to it.
    <a href="http://www.thisissudoku.com"; target="_blank">This Is Sudoku, a logic-based sudoku game</a>
  • chaudhariabhijitchaudhariabhijit Posts: 23New Users @
    not working for me
  • marketshowmarketshow Posts: 101Registered Users @ @
    Hello, i would do the following :

    - Declare all your UITextFields
    e.g. UITextField *txt1
    and the declare an extra UITextField *selectedLookupObject which is the textfield you are writing each time.

    - In : - (BOOL)textFieldShouldClear:(UITextField *)textField assign : selectedLookupObject = textField

    Then use :

    if ([selectedLookupObject isEqual:txt1])

    to do your comparison and do whatever you want.

    Hope it helps.



    Oroskopos.gr (Lite Version)
    Oroskopos.gr (Full Version)
    iQBubblez
    GO!Enterprise
Sign In or Register to comment.