【问题标题】:UITextField becomes first responder by itself...?UITextField 自己成为第一响应者......?
【发布时间】:2014-01-23 13:21:40
【问题描述】:

这只发生在设备上而不是模拟器上。..

我有两个自定义视图,其中都有一个 UITextField(Child1 和 Child2)。 这两个视图都放置在另一个 UIView(比如 viewA)上。 现在我的要求是,当在其中一个文本字段中输入文本时,我需要清除其他文本字段的内容,因此在 textFieldDidChange: 方法中我通知 viewA,然后它遍历其子视图找到 Child1 并设置其属性。但是,一旦我访问此 Child1 的 textField 以启用其 userInteraction 或将其文本设置为 nil。此文本字段现在成为第一响应者。

我不太确定它为什么会这样。您可以查看以下代码以获取更多信息。

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

自定义子视图中的方法

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

【问题讨论】:

    标签: ios iphone uitextfield


    【解决方案1】:

    不要将视图对象设置为 nil,因为它们是活动的并且您的控制器仍然处于活动状态, 尝试设置 _countTextField.text = @""; 使您的文本字段变为空。

    【讨论】:

      【解决方案2】:

      只是一个建议:

      1) 您可以将标签分配给子视图和 uitextfields,而不是手动迭代子视图,例如:

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

      然后通过以下方式从父 viewA 获取子引用:

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

      2) 将子视图文本字段委托设置为通用视图控制器

      3) 处理一单

        - (void)textFieldDidBeginEditing:(UITextField *)textField
      

      4) 在这个方法里面检查

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

      希望对你有帮助!

      【讨论】:

      • 为什么这个行得通,而不是我写的那个..?请用一些推理备份你的答案...
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-21
      • 1970-01-01
      • 2013-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多