处理 applicationDidBecomeActive -“视图控制器如何响应应用程序变为活动状态?”
我的主 AppDelegate.m 类中有 UIApplicationDelegate
协议,并定义了 applicationDidBecomeActive
方法。
我想在应用程序从后台返回时调用一个方法,但该方法位于另一个视图控制器中。如何检查 applicationDidBecomeActive 方法中当前显示的视图控制器,然后调用该控制器中的方法?
I have the UIApplicationDelegate
protocol in my main AppDelegate.m class, with the applicationDidBecomeActive
method defined.
I want to call a method when the application returns from the background, but the method is in another view controller. How can I check which view controller is currently showing in the applicationDidBecomeActive
method and then make a call to a method within that controller?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
应用程序中的任何类都可以成为应用程序中不同通知的“观察者”。当您创建(或加载)视图控制器时,您需要将其注册为 UIApplicationDidBecomeActiveNotification 的观察者,并指定当通知发送到您的应用程序时要调用哪个方法。
不要忘记自己清理干净!当您的视图消失时,请记住将自己从观察者中删除:
有关 通知中心。
Any class in your application can become an "observer" for different notifications in the application. When you create (or load) your view controller, you'll want to register it as an observer for the
UIApplicationDidBecomeActiveNotification
and specify which method that you want to call when that notification gets sent to your application.Don't forget to clean up after yourself! Remember to remove yourself as the observer when your view is going away:
More information about the Notification Center.
Swift 3、4 等效项:
添加观察者
删除观察者
回调
Swift 3, 4 Equivalent:
adding observer
removing observer
callback
Swift 2 等效项:
Swift 2 Equivalent:
斯威夫特5
Swift 5
Swift 4.2
添加观察者-
删除观察者-
处理事件-
Swift 4.2
Add observer-
Remove observer-
Handle Event-
在 Swift 4 中,Apple 通过新的编译器警告建议我们避免在这种情况下使用
#selector
。以下是实现此目的的更安全的方法:首先,创建一个将保存观察者实例的变量(将用于取消它):
然后创建一个可由通知使用的惰性变量:
如果您需要实际的要包含通知,只需将
_
替换为notification
即可。接下来,我们设置通知以观察应用程序是否处于活动状态。
这里最大的变化是,我们现在调用上面创建的 var,而不是调用 #selector。这可以消除无效选择器崩溃的情况。
最后,我们移除观察者。
With Swift 4, Apple advises via a new compiler warning that we avoid the use of
#selector
in this scenario. The following is a much safer way to accomplish this:First, create a variable that will hold the observer instance (that will be used to cancel it):
Then create a lazy var that can be used by the notification:
If you require the actual notification be included, just replace the
_
withnotification
.Next, we set up the notification to observe for the app becoming active.
The big change here is that instead of calling a
#selector
, we now call the var created above. This can eliminate situations where you get invalid selector crashes.Finally, we remove the observer.
组合方式:
The Combine way:
Swift 5 版本:
不再需要删除观察者< /a> 在 iOS 9 及更高版本中。
Swift 5 version:
Removing the observer is no longer required in iOS 9 and later.
在 Swift 5 中
In Swift 5
如果你们中有人使用 SwiftUI:
If any of you is using SwiftUI:
更清洁的 Swift 5+ 解决方案
将观察者添加到
init
或viewDidLoad
中:您不需要像其他答案所建议的那样删除观察者。 它将自动完成。
Cleaner Swift 5+ solution
Add the observer to
init
orviewDidLoad
:You don't need to remove the observer as other answers suggest. It will be done automatically.
对于 Swift5 MacOS,您需要使用 NSApplication 而不是 UIApplication。
For Swift5 MacOS, you need to use NSApplication instead of UIApplication.
在 SwiftUI 中我们可以使用 ScenePhase 来观察这些变化。例如
,现在,一旦您将应用程序置于后台,则将发生以下事件 --
现在,一旦将应用程序置于前台,则将发生以下事件 --
您可以使用从
访问此
、scenePhase
环境对象WindowsGroupScene
和任何View
。ScenePhase 符合 Comparable 并有以下情况 -
In SwiftUI we can use the
ScenePhase
to observe these changes. e.gNow once you put you application to background then following events will happen --
Now once you put you application to foreground then following events will happen --
You can use access this
scenePhase
environment object fromWindowsGroup
,Scene
and from anyView
.ScenePhase conforms to Comparable and have the following cases -