【问题标题】:UITextField delegate method not being called (shouldChangeCharactersInRange)未调用 UITextField 委托方法 (shouldChangeCharactersInRange)
【发布时间】:2011-10-29 10:15:48
【问题描述】:

即使我更改了文本字段中的文本,也不会调用此 UITextField 委托方法。 当键盘语言设置为日语/中文输入并且我从建议的单词列表中选择单词时,就会发生这种情况。

- (BOOL) textField : (UITextField *) textField shouldChangeCharactersInRange : (NSRange) range replacementString : (NSString *) string

我不确定这是否是 iOS 中的错误。

有人遇到同样的问题吗?

【问题讨论】:

  • 这听起来像是iOS的一个错误。您使用的是哪个版本的 iOS?如果您正在输入英语,是否会调用此委托?
  • 我在模拟器上发现了这个,很可能是 4.3。当它是英语时调用这个委托。当您从建议列表中选择单词时,它不会被调用,就像在 iPad 中一样。

标签: ios input keyboard uitextfield


【解决方案1】:

我确实认为这是 iOS 4.x 中的一个错误,它似乎已在 iOS 5 测试版中得到修复。

由于有关 iOS 5 的保密协议,我无法透露更多信息。希望对您有所帮助。

【讨论】:

  • 不客气。如果您认为此答案有用,请投票或选择它作为正确答案。
【解决方案2】:

这可能不是 iOS 4.x 的 bug。即使在 iOS 9.x 中,这种情况仍然存在。
对于日语/中文或任何其他可以从键盘上方的建议单词列表中选择单词的语言,此委托方法

- (BOOL) textField : (UITextField *) textField shouldChangeCharactersInRange : (NSRange) range replacementString : (NSString *) string

不会被调用。UITextfield 的文本直接由所选单词附加。

一种解决方法是使用UITextFieldTextDidChangeNotification

 [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(checkForTextfield:)
                                             name:UITextFieldTextDidChangeNotification
                                           object:nil];    

受影响的文本字段存储在通知的对象参数中。不使用 userInfo 字典。

-  (void)checkForTextfield:(NSNotification *)noti{
    UITextField* textField = noti.object;
    //do whatever you want with the UITextfield
}  

并且不要忘记在 dealloc 时将 self 从 NSNotificationCenter 中删除:

- (void)dealloc{
   [[NSNotificationCenter defaultCenter] removeObserver:self];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-26
    • 2018-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-12
    相关资源
    最近更新 更多