0

我已经在滚动视图中放置了动态创建的文本字段。我需要为滚动视图设置 contentOffset。在 textfield shouldbegin Editing 中,我给出了滚动视图。如果我遍历完成按钮,它工作正常。但是在中间,如果我点击任何文本字段,该文本字段就会上升,并且我看不到该文本字段。它显示下一个字段偏移量,但光标位于正确的文本字段中。我的代码是

  -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField

{

if(enteredHealthyHeart)
{
    for(int i=0;i<[healthyHeart_TxtFldAry count];i++)
    {
        if(textField == [healthyHeart_TxtFldAry objectAtIndex:i])
        {
            getHHTag=textField.tag;
            NSLog(@"getHHTag %i",getHHTag);
            UITextField *tempTxtFld=[healthyHeart_TxtFldAry objectAtIndex:getHHTag];

            if(i<([healthyHeart_TxtFldAry count]-1))
            {
                int j =tempTxtFld.frame.origin.y;

                healthyHeartScrollView.contentOffset=CGPointMake(0 , j);

                NSLog(@"j>>>>>%i",j);
            }
            if(i==([healthyHeart_TxtFldAry count]-1))
            {
                healthyHeartScrollView.contentOffset=CGPointMake(0 ,tempTxtFld.frame.origin.y);

            }

            [tempTxtFld resignFirstResponder];

            return YES;

        }
    }

}

}

请帮我解决这个问题。

4

2 回答 2

1

首先为每个 UITextField 添加标签;

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    //Take reference of all UITextField u added let say u have 3;
    UITextField *txt = (UItextField *)[self.view viewWithTag:99];
    UITextField *txt1 = (UItextField *)[self.view viewWithTag:199];
    UITextField *txt2 = (UItextField *)[self.view viewWithTag:299];

    if(textField == txt)
    {
       [txt1 resignFirstResponder];
       [txt2 resignFirstResponder];
       //required scroll view offset here
    }
    if(textField == txt1)
    {
       [txt resignFirstResponder];
       [txt2 resignFirstResponder];
       //required scroll view offset here
    }
    if(textField == txt2)
    {
       [txt resignFirstResponder];
       [txt1 resignFirstResponder];
       //required scroll view offset here
    }
    return YES;
}
于 2012-09-05T07:17:20.687 回答
0

scrollview.contentOffset是内容视图从原始滚动视图偏移的点。在你的代码中

healthyHeartScrollView.contentOffset=CGPointMake(0 ,tempTxtFld.frame.origin.y);

tempTxtFld超出 的内容视图healthyHeartScrollView。这使得tempTxtFld.frame.orgin.y相对于 contentview 为负数healthyHeartScrollView。所以你看不到。

附言。我真的不知道你想做什么。-_-

于 2012-09-05T07:27:20.533 回答