如何检查多个特定的UITextField?

时间:2013-05-02 05:07:51

标签: ios if-statement ios6 uitextfield

原始问题: 我有3个UITextFields(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;
    }
}

2 个答案:

答案 0 :(得分: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];
        }
    }
}

答案 1 :(得分: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];
              }
         }
}