自动更正中文字符时,会调用UITextFieldTextDidChangeNotification两次

时间:2016-05-04 03:46:53

标签: ios objective-c uitextfield autocorrect uikeyboardtype

我有init方法,我正在设置我的观察者,

  [[NSNotificationCenter defaultCenter] addObserver:self
                                        selector:@selector(textChanged:)
                                            name:UITextFieldTextDidChangeNotification
                                          object:textField];

因此,当用户从自动更正中输入中文字符时,它会调用以下方法两次,但是当我从键盘输入英文字母时,它只调用此方法一次,

- (void)textChanged:(NSNotification *)notification
{

}

任何想法我怎么能避免这个?

您好我为自动更正的中文字符&做了一些回溯英国人,他们做了不同的过程。

自动更正中文字符的回溯

1输入

  frame #6: 0x0000000102dfb109 Foundation`-[NSNotificationCenter postNotificationName:object:userInfo:] + 66
  frame #7: 0x0000000104585ab8 UIKit`-[UITextField fieldEditorDidChange:] + 257
  frame #8: 0x0000000104590cb7 UIKit`-[UITextInputController _sendDelegateChangeNotificationsForText:selection:] + 138
  frame #9: 0x0000000104598751 UIKit`-[UITextInputController setMarkedText:selectedRange:] + 744
  frame #10: 0x0000000103d1e200 UIKit`-[UIFieldEditor setMarkedText:selectedRange:] + 38
  frame #11: 0x00000001045896bb UIKit`-[UITextField setMarkedText:selectedRange:] + 90
  frame #12: 0x0000000104144c4a UIKit`-[UIResponder(UITextInput_Internal) _setMarkedText:selectedRange:] + 91
  frame #13: 0x0000000103e432f8 UIKit`-[UIKeyboardImpl unmarkText:] + 167
  frame #14: 0x0000000103e45df6 UIKit`__59-[UIKeyboardImpl handleAcceptedCandidate:executionContext:]_block_invoke_2 + 39

2输入

  frame #6: 0x0000000102dfb109 Foundation`-[NSNotificationCenter postNotificationName:object:userInfo:] + 66
  frame #7: 0x0000000104585ab8 UIKit`-[UITextField fieldEditorDidChange:] + 257
  frame #8: 0x0000000104590cb7 UIKit`-[UITextInputController _sendDelegateChangeNotificationsForText:selection:] + 138
  frame #9: 0x000000010458970e UIKit`-[UITextField unmarkText] + 48
  frame #10: 0x0000000104144d8a UIKit`-[UIResponder(UITextInput_Internal) _unmarkText] + 61
  frame #11: 0x0000000103e43203 UIKit`-[UIKeyboardImpl unmarkText] + 43
  frame #12: 0x0000000103e43305 UIKit`-[UIKeyboardImpl unmarkText:] + 180
  frame #13: 0x0000000103e45df6 UIKit`__59-[UIKeyboardImpl handleAcceptedCandidate:executionContext:]_block_invoke_2 + 39

自动更正英文字符的回溯

frame #6: 0x0000000102dfb109 Foundation`-[NSNotificationCenter postNotificationName:object:userInfo:] + 66
frame #7: 0x0000000104585ab8 UIKit`-[UITextField fieldEditorDidChange:] + 257
frame #8: 0x0000000104590cb7 UIKit`-[UITextInputController _sendDelegateChangeNotificationsForText:selection:] + 138
frame #9: 0x0000000104593281 UIKit`-[UITextInputController _insertText:fromKeyboard:] + 893
frame #10: 0x0000000104593bcd UIKit`-[UITextInputController insertText:] + 365
frame #11: 0x0000000103d1d728 UIKit`-[UIFieldEditor insertFilteredText:] + 800
frame #12: 0x000000010458a7d7 UIKit`-[UITextField insertFilteredText:] + 103
frame #13: 0x0000000103e48a83 UIKit`-[UIKeyboardImpl insertText:] + 125
frame #14: 0x0000000103e45278 UIKit`-[UIKeyboardImpl performKeyboardOutput:] + 496
frame #15: 0x0000000103e44e65 UIKit`__55-[UIKeyboardImpl handleKeyboardInput:executionContext:]_block_invoke_2 + 164

1 个答案:

答案 0 :(得分:0)

您可以两次调用init方法。 另外,请不要忘记删除[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];

中的观察者viewWillDisappear: