我有一个视图控制器,它使用NSTimer
来执行一些代码。
检测应用何时进入后台以便暂停计时器的最佳方法是什么?
Answers:
当应用程序进入后台时,您可以对任何感兴趣的类进行接收通知。这是将这些类与AppDelegate耦合的好选择。
当初始化所述类时:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillTerminate:) name:UIApplicationWillTerminateNotification object:nil];
回应通知
-(void)appWillResignActive:(NSNotification*)note
{
}
-(void)appWillTerminate:(NSNotification*)note
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillTerminateNotification object:nil];
}
在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) {
}
对于那些希望在Swift中做到这一点的人:
开init
:
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(applicationWillResignActive), name: UIApplicationWillResignActiveNotification, object: nil)
开deinit
:
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIApplicationWillResignActiveNotification, object: nil)
响应通知:
dynamic private func applicationWillResignActive() {
// Do things here
}
Apple鼓励我们在Swift中尽可能避免使用动态分派和Objective-C选择器,但这仍然是最方便的方法。
在快速4.1中:
我使用封闭版本:
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)
}
该addObserver
方法返回一个不透明的对象,需要在某个时候将其删除。
- (void)applicationWillResignActive:(UIApplication *)application
在您的应用程序委托上。您还可以注册UIApplicationWillResignActiveNotification
其他对象的通知。
不过,您不一定需要暂停计时器。如果您不执行任何操作,则该应用程序仍将进入睡眠状态,并且不会执行任何代码。大概您的计时器会在您再次活跃时触发(如果您这样做)。如果您需要做一些特别的事情,则可以注册“活跃”委托方法和通知。
斯威夫特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
}
这是使用闭包的更好的解决方案
宣布观察员
var backgroundObserver: NSObjectProtocol?
在viewDidLoad中初始化观察者
backgroundObserver = NotificationCenter.default.addObserver(forName: UIApplication.willResignActiveNotification, object: nil, queue: .main) { [weak self] notification in
// Do what you want to do when app would go to background/ resign active
}
不要忘记在deinit中删除观察者
deinit {
if let observer = backgroundObserver {
NotificationCenter.default.removeObserver(observer)
}
}
@selector
,即替换@selector(appWillResignActive)
为@selector(appWillResignActive:)
(与相同@selector(appWillTerminate:)
)。