Cal*_*vin 172 iphone multitasking uiapplicationdelegate ios4 ios
我UIApplicationDelegate
在我的主AppDelegate.m类中有协议,applicationDidBecomeActive
定义了方法.
我想在应用程序从后台返回时调用方法,但该方法在另一个视图控制器中.如何检查applicationDidBecomeActive
方法中当前显示的视图控制器,然后调用该控制器中的方法?
Ree*_*sen 296
应用程序中的任何类都可以成为应用程序中不同通知的"观察者".当你创建(或负载)视图控制器,你会希望把它注册作为观察员UIApplicationDidBecomeActiveNotification
,并指定你想要的方法时,该通知被发送到你的应用程序来调用.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(someMethod:)
name:UIApplicationDidBecomeActiveNotification object:nil];
Run Code Online (Sandbox Code Playgroud)
不要忘记自己清理!当您的视图消失时,请记住将自己移除为观察者:
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIApplicationDidBecomeActiveNotification
object:nil];
Run Code Online (Sandbox Code Playgroud)
有关通知中心的更多信息.
igr*_*rek 62
Swift 3,4等效:
添加观察者
NotificationCenter.default.addObserver(self,
selector: #selector(applicationDidBecomeActive),
name: .UIApplicationDidBecomeActive, // UIApplication.didBecomeActiveNotification for swift 4.2+
object: nil)
Run Code Online (Sandbox Code Playgroud)
删除观察者
NotificationCenter.default.removeObserver(self,
name: .UIApplicationDidBecomeActive, // UIApplication.didBecomeActiveNotification for swift 4.2+
object: nil)
Run Code Online (Sandbox Code Playgroud)
打回来
@objc func applicationDidBecomeActive() {
// handle event
}
Run Code Online (Sandbox Code Playgroud)
Zor*_*ayr 16
Swift 2等效:
let notificationCenter = NSNotificationCenter.defaultCenter()
// Add observer:
notificationCenter.addObserver(self,
selector:Selector("applicationWillResignActiveNotification"),
name:UIApplicationWillResignActiveNotification,
object:nil)
// Remove observer:
notificationCenter.removeObserver(self,
name:UIApplicationWillResignActiveNotification,
object:nil)
// Remove all observer for all notifications:
notificationCenter.removeObserver(self)
// Callback:
func applicationWillResignActiveNotification() {
// Handle application will resign notification event.
}
Run Code Online (Sandbox Code Playgroud)
斯威夫特 5
fileprivate func addObservers() {
NotificationCenter.default.addObserver(self,
selector: #selector(applicationDidBecomeActive),
name: UIApplication.didBecomeActiveNotification,
object: nil)
}
fileprivate func removeObservers() {
NotificationCenter.default.removeObserver(self, name: UIApplication.didBecomeActiveNotification, object: nil)
}
@objc fileprivate func applicationDidBecomeActive() {
// here do your work
}
Run Code Online (Sandbox Code Playgroud)
在 Swift 4 中,Apple 通过新的编译器警告提供建议,我们避免#selector
在这种情况下使用。以下是实现此目的的更安全的方法:
首先,创建一个可以被通知使用的惰性变量:
lazy var didBecomeActive: (Notification) -> Void = { [weak self] _ in
// Do stuff
}
Run Code Online (Sandbox Code Playgroud)
如果您需要实际通知被收录,只需更换_
用notification
。
接下来,我们设置通知以观察应用程序是否处于活动状态。
func setupObserver() {
_ = NotificationCenter.default.addObserver(forName: .UIApplicationDidBecomeActive,
object: nil,
queue:.main,
using: didBecomeActive)
}
Run Code Online (Sandbox Code Playgroud)
这里的重大变化是#selector
我们现在调用上面创建的 var而不是调用 a 。这可以消除无效选择器崩溃的情况。
最后,我们移除观察者。
func removeObserver() {
NotificationCenter.default.removeObserver(self, name: .UIApplicationDidBecomeActive, object: nil)
}
Run Code Online (Sandbox Code Playgroud)
斯威夫特4.2
添加观察者-
NotificationCenter.default.addObserver(self, selector: #selector(handleEvent), name: UIApplication.didBecomeActiveNotification, object: nil)
Run Code Online (Sandbox Code Playgroud)
删除观察者-
NotificationCenter.default.removeObserver(self, name: UIApplication.didBecomeActiveNotification, object: nil)
Run Code Online (Sandbox Code Playgroud)
处理事件
@objc func handleEvent() {
}
Run Code Online (Sandbox Code Playgroud)
斯威夫特 5版本:
NotificationCenter.default.addObserver(self,
selector: #selector(loadData),
name: UIApplication.didBecomeActiveNotification,
object: nil)
Run Code Online (Sandbox Code Playgroud)
iOS 9 及更高版本中不再需要删除观察者。
小智 5
在 Swift 5 中
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(applicationWillResignActive), name: UIApplication.willResignActiveNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self, name: UIApplication.willResignActiveNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIApplication.didBecomeActiveNotification, object: nil)
}
@objc private func applicationWillResignActive() {
}
@objc private func applicationDidBecomeActive() {
}
Run Code Online (Sandbox Code Playgroud)