从 AppDelegate.m 中的 applicationDidEnterBackground 中删除键盘
我的 MainViewController.m
文件中有一个带有 Textfields 的应用程序。该文件中还有一个滚动视图,因此当键盘出现时,视图会滚动,以便用户可以看到文本字段。当用户点击屏幕时,键盘将消失。一切都运行良好,除了用户点击主页按钮将应用程序置于后台然后再返回的情况。在这种情况下,键盘仍然处于打开状态,但我的滚动视图处于向下状态,并且隐藏了文本字段。理想情况下,我希望键盘也被关闭。
经过研究,调用的方法都在 AppDelegate.m 文件中(不幸的是,它没有进入 ViewDidLoad 或任何 View 生命周期方法)。如何从 AppDelegate.m 文件中的 applicationDidEnterBackground 中关闭键盘? 我是一个新手 - 我尝试在我的 MainViewController 文件中创建 +dismisskeyboard 函数并从 Appdelegate 调用它,但我的 Textfields 都是实例变量,这不起作用。我还尝试在 AppDelegate 文件中创建一个文本字段,然后执行此操作 -
[_someField 成为FirstResponder];
[_someField resignFirstResponder];
但这也不起作用...我不知道如何将故事板上的任何内容链接到 someField 的 AppDelegate 属性。
谁能提出解决这个问题的正确方法?
I have an application with Textfields in my MainViewController.m
file. There is also a scrollview in that file, so when the keyboard comes up, the view scrolls so the user can see the textfield. The keyboard is dismissed when the user taps on the screen. Everything is working well EXCEPT in the case that the user hits the home button to put the app in the background and then comes back to it. In this case, the keyboard is still up, but my scrollview is down with textfields hidden. Ideally I would like to have the keyboard be dismissed as well.
Having looked into it, the methods that are called are all in the AppDelegate.m
file (unfortunately it does not go into ViewDidLoad or any of the View lifecycle methods). How do I dismiss the keyboard from applicationDidEnterBackground in the AppDelegate.m file?
I am kind of a newbie - I have tried making a +dismisskeyboard function in my MainViewController file and calling it from the Appdelegate, but my Textfields are all instance variables and that does not work. I have also tried to create a textfield in my AppDelegate file and then do this -
[_someField becomeFirstResponder];
[_someField resignFirstResponder];
but this also does not work... I can't figure out how to link anything on my storyboard to the AppDelegate property of someField.
Can anyone suggest the correct approach to tackle this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需在
MainViewController
类中注册一个UIApplicationDidEnterBackgroundNotification
方法,然后关闭键盘即可。例如注册通知
然后添加此方法
Just register a method for
UIApplicationDidEnterBackgroundNotification
in yourMainViewController
class and dismiss your keyboard there. e.g.Register for the notification
then add this method