textViewDidEndEditing:未调用
我正在以编程方式将 UITextField 替换为 UITextView,但这似乎破坏了 Apple 代码中的某些内容。
进行交换的类是两者的委托。 FIELD 上的所有委托方法都可以正常工作 - 我使用“didBeginEditing”来触发 FIELD 的交换 ->看法。
VIEW 上的 didBeginEditing / shouldBeginEditing 方法也被正确调用。
但是... VIEW 委托上的 shouldEndEditing / didEndEditing 方法永远不会被调用。无论我如何移除焦点,它们都不会被调用(我给它们设置了断点,也带有日志语句)。例如,以下工作均无效:
- 点击屏幕上的不同文本字段(FIELD 委托获取“shouldBegin”和“didBegin” - VIEW 委托什么也得不到)
- 在背景视图上使用手势识别器来调用“resignFirstResponder” ” 对于屏幕上的每个元素(如果选择了不同的字段,则 FIELD 委托将获取“shouldEnd”和“didEnd”。即使选择了 VIEW,也不会获取任何内容
)调用 should/did begin 方法的事实表明委托已正确分配,并且运行正常 - 但为什么/如何忽略 should/did end 方法?就好像 Apple 在检测这些方法是否存在的代码中存在错误。
注意:我使用 Xcode 自动完成/内容辅助来创建方法,所以我确信没有拼写错误。需要明确的是:
-(BOOL)textViewShouldEndEditing:(UITextView *)textView
{
textComments.text = textView.text;
[textView.superview insertSubview:self.textComments aboveSubview:textView];
[textView removeFromSuperview];
return TRUE;
}
-(void)textViewDidEndEditing:(UITextView *)textView
{
textComments.text = textView.text;
[textView.superview insertSubview:self.textComments aboveSubview:textView];
[textView removeFromSuperview];
}
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
NSLog(@"blah" );
return TRUE;
}
-(void)textViewDidBeginEditing:(UITextView *)textView
{
NSLog(@"blah" );
}
I'm programmatically replacing a UITextField with a UITextView, but this seems to break something in Apple's code.
The class that does the swap-over is the delegate for both. All the delegate methods on the FIELD work correctly - I use "didBeginEditing" to trigger the swap from FIELD -> VIEW.
The didBeginEditing / shouldBeginEditing methods on the VIEW are also invoked correctly.
However ... the shouldEndEditing / didEndEditing methods on the VIEW delegate are never invoked. It doesn't matter how I remove focus, they are never called (I have them breakpointed, with log statements too). e.g. NONE of the following work:
- Tap a different textfield on the screen (FIELD delegate gets "shouldBegin" and "didBegin" - VIEW delegate gets nothing)
- Use a gesture recognizer on a background view to invoke "resignFirstResponder" for each element on screen (if a different field is selected, then FIELD delegate gets "shouldEnd" and "didEnd". VIEW gets nothing even if it's selected)
The fact that should/did begin methods are called shows that the delegate has been assigned OK, and is functioning correctly - but why / how are the should/did end methods being ignored? It's as if Apple has a bug in their code for detecting the existence of those methods.
NB: I used Xcode autocomplete / content-assist to create the methods, so I'm confident there's no typos. Just to be clear:
-(BOOL)textViewShouldEndEditing:(UITextView *)textView
{
textComments.text = textView.text;
[textView.superview insertSubview:self.textComments aboveSubview:textView];
[textView removeFromSuperview];
return TRUE;
}
-(void)textViewDidEndEditing:(UITextView *)textView
{
textComments.text = textView.text;
[textView.superview insertSubview:self.textComments aboveSubview:textView];
[textView removeFromSuperview];
}
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
NSLog(@"blah" );
return TRUE;
}
-(void)textViewDidBeginEditing:(UITextView *)textView
{
NSLog(@"blah" );
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我终于找到了原因:一些第三方代码正在订阅Apple的keyboard-did-appear/disapper通知,并将自身作为侦听器删除。
不幸的是,Apple 的 NSNotificationCenter 无法删除单个侦听器/回调选择器 - 它必须删除给定通知模式上给定实例的所有回调。
因此,作为一个副作用,这个第 3 方代码删除了我的回调选择器,尽管我自己的代码没有删除它。我正在键盘通知中设置/重置代表。对于文本字段和文本视图,Apple 触发键盘出现/消失方法的确切时间似乎略有不同(一致),这就是为什么该行为仅影响一种类型而不影响另一种类型。
我的猜测是,多米尼克可能会遇到类似的问题 - 并且他重新设置委托的调用“轻推”了某些通知的时间(意外地),从而修复了它。
I finally found the cause: some 3rd-party code was subscribing to Apple's keyboard-did-appear/disapper notifications, and removing itself as a listener.
Unfortunately, Apple's NSNotificationCenter cannot remove a single listener / callback selector - it has to remove all the callbacks of a given instance on a given notification pattern.
So, as a side-effect, this 3rd-party code was removing my callback selector, even though none of my own code removed it. I was setting / resetting delegates in the keyboard notifications. The exact timings that Apple fires-off keyboard appear/disappear methods seems to be slightly different (consistently) for textfields and textviews, and that was why the behaviour only affected one type not the other.
My guess is that Dominic might be seeing a similar problem - and that his call to re-set the delegate "nudges" the timing of some notifications (accidentally), thereby fixing it.
我今天也遇到了同样的问题。它只发生在我的 iPad 上,而不是模拟器中。它并不总是发生在 iPad 上,但大多数情况下都会发生。
我通过“重申”委托来修复它......听起来像是无稽之谈,因为它是在委托方法本身内分配的,但它对我有用。
I had today the same problem. It occured only on my iPad, not in the simulator. It occured on the iPad not always, but most.
I fixed it by "refirming" the delegate ... sounds like nonsens, because it is assigned within the delegate-method itself, but it works for me.
我相信您需要在以下方法中返回 true:
textViewShouldEndEditing
。如果您不返回,您应该收到警告,因为此方法假定返回 BOOL
I beleive you need to return true in the following method:
textViewShouldEndEditing
.You should be getting a warning if you are not returning, because this method suppose to return a BOOL