1

原始问题:我有 3 个 UITextField(nameField、locationField 和 discriptionField)我只需要在 discriptionField 上有一个 if 语句触发器,但是我尝试过的方式(如下配置)所有 3 个文本字段都执行 setViewMovedUp。我也尝试过 if([sender isEqual: discriptionField]) 但我遇到了同样的问题,所有 3 个文本字段都执行该方法。

-(void)textFieldDidBeginEditing:(UITextField *)sender
{
    if (sender == discriptionField)
    {
        //move the main view, so that the keyboard does not hide it.
        if  (self.view.frame.origin.y >= 0)
        {
           [self setViewMovedUp:YES];
        }
    }
}

我的解决方案:初学者的错误,我在没有意识到的情况下从另一个方法调用相同的方法。可能是解决问题的糟糕方法,但这是我的解决方案。

BOOL onlyDiscription = NO;

-(void)textFieldDidBeginEditing:(UITextField *)sender
    {
        if ([sender isEqual:discriptionField])
        {
            //move the main view, so that the keyboard does not hide it.
            if  (self.view.frame.origin.y >= 0)
        {
            [self setViewMovedUp:YES];
            onlyDiscription = YES;
        }
    }
}
-(void)keyboardWillShow {
    if (onlyDiscription) {
        // Animate the current view out of the way
        if (self.view.frame.origin.y >= 0)
        {
            [self setViewMovedUp:YES];
        }
        else if (self.view.frame.origin.y < 0)
        {
            [self setViewMovedUp:NO];
        }
    }
}

-(void)keyboardWillHide {
    if (onlyDiscription) {
        if (self.view.frame.origin.y >= 0)
        {
            [self setViewMovedUp:YES];
        }
        else if (self.view.frame.origin.y < 0)
        {
            con = YES;
            [self setViewMovedUp:NO];
        }
    onlyDiscription = NO;
    }
}
4

2 回答 2

3

最好使用标签值并与标签值进行比较。

-(void)textFieldDidBeginEditing:(UITextField *)sender
{
    if (sender.tag == discriptionField.tag)
    {
        //move the main view, so that the keyboard does not hide it.
        if  (self.view.frame.origin.y >= 0)
        {
           [self setViewMovedUp:YES];
        }
    }
}

(或者)

-(void)textFieldDidBeginEditing:(UITextField *)sender
{
    if([sender isEqual:discriptionField]){
        if  (self.view.frame.origin.y >= 0)
        {
          [self setViewMovedUp:YES];
        }
    }
}
于 2013-05-02T05:10:54.917 回答
0

请像这样检查

-(void)textFieldDidBeginEditing:(UITextField *)sender
{
        if ([sender isEqual:discriptionField])
        {
             //move the main view, so that the keyboard does not hide it.
              if  (self.view.frame.origin.y >= 0)
              {
                   [self setViewMovedUp:YES];
              }
         }
}
于 2013-05-02T05:13:23.390 回答