简体   繁体   中英

UITextField becomes first responder by itself…?

This happens only on device and not on simulator..

I have two custom views and both have a UITextField in them(Child1 and Child2). Both these views are placed on another UIView (Say viewA). Now my requirement is such that when text is entered in one of the textfield I need to clear the other text fields content, so in the textFieldDidChange: method I inform viewA and than it iterates over its subview finds Child1 and sets its properties. But as soon as I access the textField of this Child1 to enable its userInteraction or and set its text to nil. This textfield now becomes the first responder.

I am not really sure why it does that. You can look at the below code to get more info.

Method inside viewA:

  for (UIView *view in [self subviews])
    {
        if ([view isKindOfClass:[ChildView class]])
        {
            ChildView *concernedCustomerView = (ChildView *)view;
            if (concernedCustomerView.typeOfCompartment == CompartmentTypeNone)
            {
                [concernedCustomerView.checkBoxButton setSelected:NO];
                [concernedCustomerView.countSwitch setOn:NO animated:YES];
                concernedCustomerView.countTextField.userInteractionEnabled = YES;
                concernedCustomerView.countTextField.alpha = 1.0f;
                concernedCustomerView.countTextField.text = nil;
            }
        }
    }

Method inside custom Child View

-(void)textFieldDidChange:(id)sender
{
    NSString *note = _countTextField.text;
    note = [note stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    //If we are checking note for nil it should be before calling trimming white space
    if (note && note.length > 0)
    {
        [_checkBoxButton setSelected:YES];
        if (note.length == 3 && note.integerValue == 999)
        {
            [_countSwitch setOn:YES animated:YES];
            _countTextField.userInteractionEnabled = NO;
            _countTextField.alpha = 0.5f;
            _countTextField.text = nil;
//           [_countSwitch sendActionsForControlEvents:UIControlEventValueChanged];
        }
    }
    else
    {
        [_checkBoxButton setSelected:NO];
    }

    if ([self.delegate conformsToProtocol:@protocol(ChildViewDelegate)] &&
        [self.delegate respondsToSelector:@selector(adjustStateOfOtherControls:andCheckBoxStatus:)])
    {
        [self.delegate adjustStateOfOtherControls:_typeOfCompartment andCheckBoxStatus:_checkBoxButton.selected];
    }
}

Do not set the view object to nil because they are alive and your controller is still active, try to set _countTextField.text = @""; so that your textfield become empty.

Just a suggest:

1) Instead of manual iterate subviews you can assign tag to child views and uitextfields eg:

child1View.tag = 100;
child2View.tag = 200;
...
textField1.tag = 10;
textField2.tag = 20;

then get child references from parent viewA by:

 UIView *child1View = [viewA viewWithTag:100];
 UIView *child2View = [viewA viewWithTag:200];

2) Set child views textfield delegate to a common viewcontroller

3) Handle one single

  - (void)textFieldDidBeginEditing:(UITextField *)textField

4) Iniside this method check

if(textField.tag==10)
{
  do stuff
}
else if(textfield.tag==20)
{
  do other stuff
}

Hope it helps !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM