0

i have 6 text field.i want to get current cursor position(cursor in which text field).like android onfocus .after i get the position i need to draw slider near to that textfield. is there any API for iPhone.guide me i'm new to iPhone and Xcode.

  • You want curson in textfield am I right? – iPhone Dec 07 '11 at 11:43
  • @iPhone i want to know curser in which textfield now?i meant textfield1 or textfield2 or textfield3 etc... like.i want to get X and Y position and draw the slider near to that textfield. –  Dec 07 '11 at 11:53
  • 1
    You can iterate through all the textfields and check if the textfield is the first responder, if so, then the cursor is there in that particular text field. – Shanti K Dec 07 '11 at 12:03
  • @Shanti Kamichetty ya i understand.is there any example?. kindly guide me any programing code.i mean getting first responder action. –  Dec 07 '11 at 12:07

2 Answers2

1
 - (void)textFieldDidEndEditing:(UITextField *)textField
 {
    [textField resignFirstResponder]; 
 }

Add your own logic for ‘for' loop here. I am using my logic. (As per my requirements).

      NSArray *subviews = [[NSArray alloc] initWithArray:self.view.subviews];
       for(UIView *subview in subviews)
        if([subview isKindOfClass:[UITextField class]])
         {
           UITextField *textField = (UITextField *) subview;
           if([textField isFirstResponder])
           {
              // do whatever you want
           }
         }
Surjit Joshi
  • 3,287
  • 2
  • 18
  • 20
0

If You want to see TextField In center OF Screen hen Use

Firstly You Add ScrollView in nid and connect and all TextField's delegete is connect with fileOwner

and Implement this code

It"s Better according to your View

#pragma mark -
#pragma mark textField Delegte

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{

    [self scrollViewToCenterOfScreen:textField];    
    return YES;

    }

- (void)scrollViewToCenterOfScreen:(UIView *)theView {  
    CGFloat viewCenterY = theView.center.y;  
    CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];  

    CGFloat availableHeight = applicationFrame.size.height - 200;            // Remove area covered by keyboard  

    CGFloat y = viewCenterY - availableHeight / 2.0;  
    if (y < 0) {  
        y = 0;  
    }  
    [scrollview setContentOffset:CGPointMake(0, y) animated:YES];  

}

-(BOOL) textFieldShouldReturn:(UITextField *)textField{

    if ([textField isEqual:textField1])
    {
        [textField2 becomeFirstResponder];
    }
    else if ([textField isEqual:textField2])
    {
        [textField3 becomeFirstResponder];
    }
    else if ([textField isEqual:textField3])
    {
        [textField4 becomeFirstResponder];
    }
    else 
    {
        [textField4 resignFirstResponder];      
        [scrollview setContentOffset:CGPointMake(0, 0) animated:YES];
    }

    return YES;
}
Deepesh
  • 633
  • 4
  • 11