从 AppDelegate.m 中的 applicationDidEnterBackground 中删除键盘

发布于 2025-01-02 21:05:01 字数 718 浏览 7 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叹沉浮 2025-01-09 21:05:01

只需在 MainViewController 类中注册一个 UIApplicationDidEnterBackgroundNotification 方法,然后关闭键盘即可。例如

注册通知

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedNotification:) name:UIApplicationDidEnterBackgroundNotification object:nil];

然后添加此方法

- (void) receivedNotification:(NSNotification *) notification
{
    [txtFld resignFirstResponder];
}

Just register a method for UIApplicationDidEnterBackgroundNotification in your MainViewController class and dismiss your keyboard there. e.g.

Register for the notification

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedNotification:) name:UIApplicationDidEnterBackgroundNotification object:nil];

then add this method

- (void) receivedNotification:(NSNotification *) notification
{
    [txtFld resignFirstResponder];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文