2014-01-23 84 views
0

這種情況僅在設備上,而不是在模擬器..UITextField本身成爲第一響應者...?

我有兩個自定義視圖並且都在他們的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]; 
    } 
} 

回答

0

不要設置視圖對象爲零,因爲他們是活的,你的控制器依舊在工作, 嘗試設置_countTextField.text = @"";以便您的文本框變爲空白。

0

只是一個建議:

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)Iniside這種方法檢查

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

希望它能幫助!

+0

爲什麼這個工作,而不是我寫的..?請用一些推理來備份你的答案...... –

相關問題