FireSky
FireSky

Reputation: 181

How do I check for a specific UITextField out of many?

Original question: I have 3 UITextFields (nameField, locationField, and discriptionField) I need to have a if statement trigger on discriptionField only, however the way I have tried it (configured below) all 3 textfields perform setViewMovedUp. I have also tried if([sender isEqual: discriptionField]) but I'm getting the same problem, all 3 textfields preform the method.

-(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];
        }
    }
}

My Solution: Beginner mistake, I was calling that same method from another method without realizing it. Probably a poor way to solve the problem but here is my solution.

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

Upvotes: 1

Views: 261

Answers (2)

Balu
Balu

Reputation: 8460

Better to use tag value and compare with tag value.

-(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];
        }
    }
}

(or)

-(void)textFieldDidBeginEditing:(UITextField *)sender
{
    if([sender isEqual:discriptionField]){
        if  (self.view.frame.origin.y >= 0)
        {
          [self setViewMovedUp:YES];
        }
    }
}

Upvotes: 3

Dharmbir Singh
Dharmbir Singh

Reputation: 17535

Please check like this

-(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];
              }
         }
}

Upvotes: 0

Related Questions