如何在iPhone文本字段Xcode 4.2中获取光标位置

时间:2011-12-07 11:41:04

标签: iphone uitextfield xcode4.2

我有6个文本字段。我想获取当前光标位置(光标在其中的文本字段)。像android onfocus。在我得到位置后我需要在该文本字段附近绘制滑块。是否有适用于iPhone的API。指导我,我是iPhone和Xcode的新手。

2 个答案:

答案 0 :(得分:1)

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

在此为循环添加'的逻辑。我正在使用我的逻辑。 (根据我的要求)。

      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
           }
         }

答案 1 :(得分:0)

如果您想在屏幕中心看到TextField,请使用

首先在nid中添加ScrollView并连接,所有TextField的delegete都与fileOwner连接

并实施此代码

根据你的观点,它会更好

#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;
}