简体   繁体   English

检测何时在导航栏中点按后退按钮

[英]Detecting when back button is tapped in navigation bar

I need to resignFirstResponder() on a text field when the user taps the back button in the navigation bar of the navigation controller, otherwise I get some error. 当用户点击导航控制器的导航栏中的后退按钮时,我需要在文本字段上使用resignFirstResponder() ,否则会出现一些错误。 The back button works as it should (the previous view gets shown) but I don't know where to do resign first responder. 后退按钮按应有的方式工作(显示了先前的视图),但我不知道在哪里辞职第一响应者。 It's too late if I do it in viewWillDisappear() (I tried), and prepareForSegue() doesn't get called, so I need to somehow do it as soon as the back button gets tapped or at least before viewWillDisappear() . 如果我在viewWillDisappear() (我尝试过)中进行操作已经太晚了,并且没有调用prepareForSegue() ,那么我需要以某种方式立即执行一下后退按钮,或者至少在viewWillDisappear()之前进行操作。 How do I detect that event? 如何检测该事件?

Note: See first comment on question for simpler answer 注意:请参阅对问题的第一条评论以获取更简单的答案

You could combine NotificationCenter and this post: Execute action when back bar button of UINavigationController is pressed to make a custom back button (that looks the same). 您可以将NotificationCenter和此文章结合起来: 当按下UINavigationController的后退按钮以创建自定义后退按钮(外观相同) 时执行操作 Just post a Notification in the action for the back button, and add an observer for the Notification on the textfield whose action calls resignFirstResponder . 只需在操作中为后退按钮发布一个Notification ,然后在其操作调用resignFirstRespondertextfield上添加Notification的观察者。 You would also need to make an image or draw an image in CoreGraphics for the arrow. 您还需要制作图像或在CoreGraphics中绘制箭头图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM