ios 检测应用程序何时进入后台查看我的视图的最佳方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9011868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the best way to detect when the app is entering the background for my view?
提问by jfisk
I have a view controller that uses an NSTimer
to execute some code.
我有一个使用 anNSTimer
来执行一些代码的视图控制器。
What's the best way to detect when the app is going to the background so I can pause the timer?
检测应用程序何时进入后台以便我可以暂停计时器的最佳方法是什么?
回答by Jesse Black
You can have any class interested in when the app goes into the background receive notifications. This is a good alternative to coupling these classes with the AppDelegate.
您可以让任何感兴趣的类在应用程序进入后台时接收通知。这是将这些类与 AppDelegate 耦合的不错选择。
When initializing said classes:
初始化所述类时:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillTerminate:) name:UIApplicationWillTerminateNotification object:nil];
Responding to the notifications
响应通知
-(void)appWillResignActive:(NSNotification*)note
{
}
-(void)appWillTerminate:(NSNotification*)note
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillTerminateNotification object:nil];
}
回答by Ashok R
In Swift 4.0
在 Swift 4.0 中
override func viewDidLoad() {
super.viewDidLoad()
let app = UIApplication.shared
//Register for the applicationWillResignActive anywhere in your app.
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.applicationWillResignActive(notification:)), name: NSNotification.Name.UIApplicationWillResignActive, object: app)
}
@objc func applicationWillResignActive(notification: NSNotification) {
}
回答by Damien
On your applications AppDelegate the (void)applicationDidEnterBackground:(UIApplication *)application
method will be called by iOS. You can stop your timer in there.
在您的应用程序 AppDelegate 上(void)applicationDidEnterBackground:(UIApplication *)application
,iOS 将调用该方法。你可以在那里停止你的计时器。
回答by Luke
For those looking to do this in Swift:
对于那些希望在 Swift 中执行此操作的人:
On init
:
在init
:
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(applicationWillResignActive), name: UIApplicationWillResignActiveNotification, object: nil)
On deinit
:
在deinit
:
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIApplicationWillResignActiveNotification, object: nil)
Responding to the notification:
对通知的回应:
dynamic private func applicationWillResignActive() {
// Do things here
}
Apple encourages us to avoid dynamic dispatch and Objective-C selectors whenever possible in Swift, but this is still the most convenient way to do this.
Apple 鼓励我们在 Swift 中尽可能避免使用动态分派和 Objective-C 选择器,但这仍然是最方便的方法。
回答by juanjo
In swift 4.1:
在 swift 4.1 中:
I use the closure version:
我使用关闭版本:
var observer: NSObjectProtocol!
// inside init or viewDidLoad:
observer = NotificationCenter.default.addObserver(forName: .UIApplicationWillResignActive, object: nil, queue: nil) { _ in
print("willResignActive")
}
deinit {
NotificationCenter.default.removeObserver(observer)
}
The addObserver
method returns an opaque object that needs to be removed at some point.
该addObserver
方法返回一个需要在某个时候删除的不透明对象。
回答by ingconti
only a side note: If you register a controller A to be notified going background, be careful that it will be called even if you (for example..) push a second controller B and You are displaying B: If this behaviour is not correct, is better to register/unregister in
只是一个旁注:如果您注册一个控制器 A 以在后台通知,请注意即使您(例如..)推送第二个控制器 B 并且您正在显示 B:如果此行为不正确,它也会被调用, 最好在
didAppear/WillDisappear.
确实出现/将消失。
回答by thexande
Swift 4:
斯威夫特 4:
init() {
NotificationCenter.default.addObserver(self,
selector: #selector(applicationWillResignActive),
name: NSNotification.Name.UIApplicationWillResignActive,
object: nil)
}
deinit {
NotificationCenter.default.removeObserver(self,
name: NSNotification.Name.UIApplicationWillResignActive,
object: nil)
}
@objc private func applicationWillResignActive() {
self.header.blur.effect = nil
}
回答by smparkes
- (void)applicationWillResignActive:(UIApplication *)application
on your app delegate. You can also register for the UIApplicationWillResignActiveNotification
notification on other objects.
- (void)applicationWillResignActive:(UIApplication *)application
在您的应用程序委托上。您还可以注册UIApplicationWillResignActiveNotification
其他对象的通知。
You don't necessarily need to pause the timer, though. If you don't do anything, the app will get put to sleep anyway and won't execute any code. Presumably your timer will fire when you become active again (if you do). If you need to do something special there are 'did become active' delegate methods and notifications you can register for as well.
不过,您不一定需要暂停计时器。如果你什么都不做,应用程序无论如何都会进入睡眠状态并且不会执行任何代码。据推测,当您再次活跃时(如果您这样做),您的计时器会触发。如果你需要做一些特别的事情,你也可以注册“did become active”委托方法和通知。