ios 为什么 textFieldDidEndEditing: 没有被调用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1228298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is textFieldDidEndEditing: not being called?
提问by Chris Cleeland
I've got a UITextField, and when text gets entered into that, I want to obtain the doubleValue from the text field and perform some computations and display them in a UITableView.
我有一个 UITextField,当输入文本时,我想从文本字段中获取 doubleValue 并执行一些计算并将它们显示在 UITableView 中。
The delegate for the UITextField adopts the UITextFieldDelegate protocol, and implements both textFieldShouldReturn: and textFieldDidEndEditing: methods. textFieldShouldReturn: resigns first responder status which, according to docs should also trigger textFieldDidEndEditing:, but I never see textFieldDidEndEditing: called.
UITextField 的委托采用 UITextFieldDelegate 协议,并实现 textFieldShouldReturn: 和 textFieldDidEndEditing: 方法。textFieldShouldReturn: 退出第一响应者状态,根据文档,该状态也应该触发 textFieldDidEndEditing:,但我从未看到 textFieldDidEndEditing: 被调用。
- (BOOL)textFieldShouldReturn:(UITextField*)theTextField {
if (theTextField == thresholdValue) {
[thresholdValue resignFirstResponder];
}
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
[self updateThresholdValue:textField];
}
It may be worth noting that I also tried connecting some of the textfield events to the delegate and having the event call updateThresholdValue: directly, but that didn't work either.
可能值得注意的是,我还尝试将一些文本字段事件连接到委托并让事件调用 updateThresholdValue: 直接,但这也不起作用。
采纳答案by Chris Cleeland
As mentioned in one of the comments, I tried replicating a simpler version of what I was trying to do using a HelloWorld app, and it worked on the first try, with no problems, just as expected. Faced with that, I began to wonder a bit.
正如其中一条评论中提到的,我尝试使用 HelloWorld 应用程序复制我尝试执行的操作的更简单版本,并且在第一次尝试时就成功了,没有任何问题,正如预期的那样。面对这种情况,我开始有点疑惑。
Somewhere in all the googling I did to try to get an answer to this problem, I ran across a link where a person was having a problem with events not firing from a button, and it turned out that the problem was related to the view in which the button was placed and that view having another subview as a peer to the button, and that subview was somehow eating the events rather than allowing them to go where they'd been directed. The scenario wasn't exactly the same as mine, but it sounded close enough that investigation would be worthwhile. Despite all the excellent advice received here, none had yet borne fruit.
在所有谷歌搜索中,我试图找到这个问题的答案,我遇到了一个链接,其中一个人遇到了事件未从按钮触发的问题,结果发现问题与按钮被放置在哪个视图中,并且该视图具有另一个子视图作为按钮的对等体,并且该子视图以某种方式吞噬了事件,而不是让它们去到它们被引导的地方。场景与我的并不完全相同,但听起来足够接近,值得进行调查。尽管这里收到了所有很好的建议,但没有一个结果。
Last night I decided to completely re-create the nib file from scratch (made a copy of what I had first), and I got it to work. The key difference seems to be that I in the original non-working nib file, I had dragged over a TableViewController
from the palette, then changed its type to my subclass PZTableViewController
. When I did that same thing in my new nib, the events didn't fire. If, on the other hand, I removed the dragged-over TableViewController
and instead just dragged over an NSObject
and changed its class to PZTableViewController
and plumbed everything up, it all "just worked".
昨晚我决定从头开始完全重新创建 nib 文件(复制我最初拥有的文件),然后我让它工作了。关键的区别似乎是我在原始的非工作 nib 文件中,我TableViewController
从调色板中拖了一个,然后将其类型更改为我的子类PZTableViewController
. 当我在我的新笔尖上做同样的事情时,事件没有触发。另一方面,如果我删除了拖过的东西TableViewController
,而是只是拖过一个NSObject
并将它的类更改为PZTableViewController
并将所有东西都连接起来,那么这一切都“正常工作”。
I will try to chase down the link that got me started on this line of thinking and post it either as an edit or as a comment.
我将尝试追踪让我开始这一思路的链接,并将其作为编辑或评论发布。
This is also not a complete answer because I do not yet understand the difference between the from-the-palette TableViewController
and my subclass. When I figure that out, I will update this answer via edit or comment.
这也不是一个完整的答案,因为我还不明白 from-the-paletteTableViewController
和我的子类之间的区别。当我弄清楚这一点时,我将通过编辑或评论更新此答案。
回答by Miha Hribar
Just ran into the same problem you described. After trying everything I could think of I added this delegate method:
刚遇到你描述的同样问题。在尝试了我能想到的一切之后,我添加了这个委托方法:
// if we encounter a newline character return
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
// enter closes the keyboard
if ([string isEqualToString:@"\n"])
{
[textField resignFirstResponder];
return NO;
}
return YES;
}
Now the textFieldShouldEndEditing fires and the text field resigns first responder.
现在 textFieldShouldEndEditing 触发,文本字段退出第一响应者。
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
回答by paulthenerd
textFieldDidEndEditing is fired when the textfield resigns it's first responder status while textFieldShouldReturn is fired when the return button is pressed.
textFieldDidEndEditing 在文本字段退出其第一响应者状态时触发,而 textFieldShouldReturn 在按下返回按钮时触发。
It soundslike your textfield is never resigning as firstResponder. You can check it pretty easily by putting some debug output (as suggested in the comments) and just navigating out of the textfield with a touch - eg start typing then just touch outside of the field to force it to resign firstResponder.
这听起来像你的文本字段是永远不会辞去firstResponder。您可以通过放置一些调试输出(如评论中建议的那样)并通过触摸导航出文本字段来轻松检查它 - 例如,开始输入然后触摸该字段外以强制它退出 firstResponder。
Not sure if that helps a lot, but it sounds like a strange case you are hitting.
不确定这是否有很大帮助,但这听起来像是您遇到的奇怪案例。
回答by karjaubayev
If you adopted UITextFieldDelegateprotocol in your view controller, then write the following line of code in viewDidLoadmethod to activate the methods of above protocol to your corresponding textFields:
如果您在视图控制器中采用了UITextFieldDelegate协议,则在viewDidLoad方法中编写以下代码行以将上述协议的方法激活到相应的文本字段:
override func viewDidLoad() {
//other stuff
yourTextField.delegate = self
}
回答by Anton Tropashko
I ended up wiring up editingChanged to record every single change to a text field rather than figuring out how to cover all the corner cases with the remaining callbacks.
我最终连接了 editChanged 来记录对文本字段的每一个更改,而不是弄清楚如何用剩余的回调覆盖所有极端情况。
Very "efficient" due to the android-like "usability" of the textFieldDidEndEditing (sarcasm).
由于 textFieldDidEndEditing(讽刺)的类似 android 的“可用性”,因此非常“高效”。