从 AppDelegate.m 中的 applicationDidEnterBackground 中去掉键盘

     2023-03-12     94

关键词:

【中文标题】从 AppDelegate.m 中的 applicationDidEnterBackground 中去掉键盘【英文标题】:Get rid of the keyboard from applicationDidEnterBackground in the AppDelegate.m 【发布时间】:2012-02-07 14:53:47 【问题描述】:

我的MainViewController.m 文件中有一个带有文本字段的应用程序。该文件中还有一个滚动视图,因此当键盘出现时,视图会滚动,以便用户可以看到文本字段。当用户点击屏幕时,键盘将被关闭。除了用户点击主页按钮将应用程序置于后台然后返回的情况外,一切都运行良好。在这种情况下,键盘仍然处于启动状态,但我的滚动视图处于关闭状态,并且隐藏了文本字段。理想情况下,我也希望关闭键盘。

经过调查,被调用的方法都在AppDelegate.m 文件中(不幸的是它没有进入 ViewDidLoad 或任何 View 生命周期方法)。如何从 AppDelegate.m 文件中的 applicationDidEnterBackground 关闭键盘? 我是一个新手——我尝试在我的 MainViewController 文件中创建一个 +dismisskeyboard 函数并从 Appdelegate 调用它,但是我的 Textfields 都是实例变量,这不起作用。我还尝试在我的 AppDelegate 文件中创建一个文本字段,然后执行此操作 -

[_someField becomeFirstResponder];

[_someField resignFirstResponder];

但这也不起作用...我不知道如何将故事板上的任何内容链接到 someField 的 AppDelegate 属性。

谁能提出解决这个问题的正确方法?

【问题讨论】:

【参考方案1】:

只需在您的 MainViewController 类中为 UIApplicationDidEnterBackgroundNotification 注册一个方法,然后在那里关闭您的键盘。例如 注册接收通知

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

然后添加这个方法

- (void) receivedNotification:(NSNotification *) notification

    [txtFld resignFirstResponder];

【讨论】:

谢谢!多么简单的解决方案!我仍在学习通知,但管理了这个,它运行良好。

如何从 AppDelegate.m 访问 tabBarController?

】如何从AppDelegate.m访问tabBarController?【英文标题】:HowtoaccesstabBarControllerfromAppDelegate.m?【发布时间】:2014-11-2520:06:03【问题描述】:这是我的故事板:我正在尝试从AppDelegate.m中的方法访问tabBarController这是AppDelegate.h:#import<UI... 查看详情

如何从 Xcode 中的 AppDelegate.m 文件更新 UILabel 的文本?

】如何从Xcode中的AppDelegate.m文件更新UILabel的文本?【英文标题】:howtoupdateaUILabel\'sTextfromAppDelegate.mfileinXcode?【发布时间】:2014-08-3014:51:50【问题描述】:我是Objective-C编程的新手。当应用程序进入后台时,我想从UILabel和至少NSLo... 查看详情

iOS AppDelegate.m:处理 openUrl RCTLinkingManager 和 Twitter - 方法 'application:openURL:options:' 的重复声明

】iOSAppDelegate.m:处理openUrlRCTLinkingManager和Twitter-方法\\\'application:openURL:options:\\\'的重复声明【英文标题】:iOSAppDelegate.m:HandlingopenUrlRCTLinkingManagerandTwitter-Duplicatedeclarationofmethod\'application:openURL:options 查看详情

如何将数据从 AppDelegate 传递到 ViewController?

】如何将数据从AppDelegate传递到ViewController?【英文标题】:HowtopassdatafromAppDelegatetoViewController?【发布时间】:2012-12-0305:18:54【问题描述】:我正在使用Safari浏览网页。单击此页面上的按钮后,我的Ipad将启动我的应用程序。所以... 查看详情

如何从 AppDelegate.m 调用 ViewController.m 方法

】如何从AppDelegate.m调用ViewController.m方法【英文标题】:HowtocallaViewController.mmethodfromtheAppDelegate.m【发布时间】:2014-01-2517:09:35【问题描述】:在我的ViewController.m中,我有无效的“保存数据”:#import"ViewController.h"@interfaceViewControll... 查看详情

无法从 appdelegate.m 为 NSProgressIndicator 设置动画

】无法从appdelegate.m为NSProgressIndicator设置动画【英文标题】:UnabletoanimateNSProgressIndicatorfromappdelegate.m【发布时间】:2013-12-1121:03:29【问题描述】:我之前在IOS上工作过,但刚刚开始在MacOsX可可应用程序上工作。我遇到了关于NSProgr... 查看详情

未修改的 AppDelegate.m 中的编译错误

】未修改的AppDelegate.m中的编译错误【英文标题】:CompileErrorsinuntouchedAppDelegate.m【发布时间】:2013-01-0420:17:37【问题描述】:我一直在创建一个简单的示例应用程序来演示在IOS中播放声音文件。为此,我创建了一个带有一个视图... 查看详情

AppDelegate.m 类中的@interface 错误

】AppDelegate.m类中的@interface错误【英文标题】:@interfaceerrorinAppDelegate.mclass【发布时间】:2012-12-1518:44:13【问题描述】:应该是我缺少的简单解决方案。我有一个选项卡视图控制器驱动的应用程序,我想在用户启动或打开应用程... 查看详情

Cordova iOS:在 AppDelegate.m 中添加方法调用

】CordovaiOS:在AppDelegate.m中添加方法调用【英文标题】:CordovaiOS:AddmethodcallinAppDelegate.m【发布时间】:2016-06-2307:25:07【问题描述】:我正在为iOS构建一个带有cordova/ionic的应用程序由于多种原因,我们必须将代码放在AppDelegate.m的... 查看详情

AppDelegate.m 文件中的两个包冲突

】AppDelegate.m文件中的两个包冲突【英文标题】:TwopackagesconflictinAppDelegate.mfile【发布时间】:2017-04-2315:12:05【问题描述】:我在我的项目中使用https://github.com/devfd/react-native-google-signin和https://github.com/luisfcofv/react-native-deep-linking包... 查看详情

AppDelegate.m 需要将数据发送到单独的 ViewController 的 WebView

】AppDelegate.m需要将数据发送到单独的ViewController的WebView【英文标题】:AppDelegate.mneedstosenddatatoaseparateViewController\'sWebView【发布时间】:2013-12-2705:43:06【问题描述】:-(BOOL)application:(UIApplication*)applicationhandleOpenURL:(NSURL*)url 查看详情

appdelegate.m 中的 UIAlertView 不起作用

】appdelegate.m中的UIAlertView不起作用【英文标题】:UIAlertViewinappdelegate.mdoesnotwork【发布时间】:2011-12-2208:40:03【问题描述】:我已经在谷歌上搜索了将近一整天的时间,但没有找到任何解决方案,所以我想问你们..:)我正在开发一... 查看详情

如何从appdelegate调用方法[重复]

】如何从appdelegate调用方法[重复]【英文标题】:Howtocallamethodfromappdelegate[duplicate]【发布时间】:2013-03-2516:37:23【问题描述】:我有一个switch语句,它利用nsuserdefaultsbool函数来确定打开和关闭。我的问题是当switch键bool为yes时,如... 查看详情

仅为 AppDelegate.m iOS 中的首次启动设置特定方向

】仅为AppDelegate.miOS中的首次启动设置特定方向【英文标题】:SetspecificorientationonlyforfirstlaunchinAppDelegate.miOS【发布时间】:2019-09-3017:18:16【问题描述】:我将这个库用于我的reactnative项目:react-native-orientation-locker我想要实现的是... 查看详情

在 AppDelegate.m 上设置 label.text

】在AppDelegate.m上设置label.text【英文标题】:Setlabel.textoverAppDelegate.m【发布时间】:2012-05-0710:28:03【问题描述】:我有ViewController,由于某些原因,我必须从AppDelegate.m设置label.text我该怎么做?我已尝试使用此代码,但没有成功:... 查看详情

从 appdelegate 设置 UIViewController 委托

】从appdelegate设置UIViewController委托【英文标题】:setUIViewControllerdelegatefromappdelegate【发布时间】:2014-03-0617:25:02【问题描述】:我正在尝试从我的应用委托中设置视图控制器的委托。但它不起作用。AppDelegate.m:UIStoryboard*sb=[UIStoryb... 查看详情

IOS 使用 AppDelegate.m 中 ViewController.m 的方法?

】IOS使用AppDelegate.m中ViewController.m的方法?【英文标题】:IOSUsingmethodfromViewController.minAppDelegate.m?【发布时间】:2013-07-0916:46:48【问题描述】:所以我在ViewController.m中有一个方法,只需按一下按钮即可发布通知(到通知中心)这... 查看详情

应用关闭时从通知中显示 UIAlertView 的问题

...【问题描述】:我有以下用于推送通知的代码:位于我的appdelegate.m中的是以下代码-(BOOL)application:(UIApplication*)applicationdidFinishLa 查看详情