Ios 处理应用程序IDBECOMEACTIVE-“文件”;视图控制器如何响应应用程序激活?”;
在我的主AppDelegate.m类中有Ios 处理应用程序IDBECOMEACTIVE-“文件”;视图控制器如何响应应用程序激活?”;,ios,iphone,ios4,multitasking,uiapplicationdelegate,Ios,Iphone,Ios4,Multitasking,Uiapplicationdelegate,在我的主AppDelegate.m类中有UIApplicationDelegate协议,定义了ApplicationIDBecMeactive方法 我想在应用程序从后台返回时调用一个方法,但该方法位于另一个视图控制器中。如何检查当前在应用程序IDBECOMEACTIVE方法中显示的视图控制器,然后调用该控制器中的方法?应用程序中的任何类都可以成为应用程序中不同通知的“观察者”。创建(或加载)视图控制器时,需要将其注册为UIApplicationIDBecMeactiveNotification的
UIApplicationDelegate
协议,定义了ApplicationIDBecMeactive
方法
我想在应用程序从后台返回时调用一个方法,但该方法位于另一个视图控制器中。如何检查当前在
应用程序IDBECOMEACTIVE
方法中显示的视图控制器,然后调用该控制器中的方法?应用程序中的任何类都可以成为应用程序中不同通知的“观察者”。创建(或加载)视图控制器时,需要将其注册为UIApplicationIDBecMeactiveNotification
的观察者,并指定在将通知发送到应用程序时要调用的方法
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(someMethod:)
name:UIApplicationDidBecomeActiveNotification object:nil];
NotificationCenter.default.addObserver(self,
selector: #selector(applicationDidBecomeActive),
name: (NSApplication.didBecomeActiveNotification),
object: nil)
}
别忘了自己打扫卫生!记住,当您的视图消失时,请删除您作为观察者的身份:
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIApplicationDidBecomeActiveNotification
object:nil];
有关.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.
}
Swift 3、4等价物: 添加观察者
NotificationCenter.default.addObserver(self,
selector: #selector(applicationDidBecomeActive),
name: .UIApplicationDidBecomeActive, // UIApplication.didBecomeActiveNotification for swift 4.2+
object: nil)
NotificationCenter.default.removeObserver(self,
name: .UIApplicationDidBecomeActive, // UIApplication.didBecomeActiveNotification for swift 4.2+
object: nil)
移除观察者
NotificationCenter.default.addObserver(self,
selector: #selector(applicationDidBecomeActive),
name: .UIApplicationDidBecomeActive, // UIApplication.didBecomeActiveNotification for swift 4.2+
object: nil)
NotificationCenter.default.removeObserver(self,
name: .UIApplicationDidBecomeActive, // UIApplication.didBecomeActiveNotification for swift 4.2+
object: nil)
回拨
@objc func applicationDidBecomeActive() {
// handle event
}
对于Swift 4,苹果通过一个新的编译器警告建议我们在这种情况下避免使用
#选择器
。以下是一种更安全的方法:
首先,创建一个可由通知使用的惰性变量:
lazy var didBecomeActive: (Notification) -> Void = { [weak self] _ in
// Do stuff
}
如果您需要包含实际通知,只需将替换为通知
接下来,我们设置通知以观察应用程序是否处于活动状态
func setupObserver() {
_ = NotificationCenter.default.addObserver(forName: .UIApplicationDidBecomeActive,
object: nil,
queue:.main,
using: didBecomeActive)
}
这里最大的变化是,我们不再调用#选择器
,而是调用上面创建的var。这可以消除出现无效选择器崩溃的情况
最后,我们移除观察者
func removeObserver() {
NotificationCenter.default.removeObserver(self, name: .UIApplicationDidBecomeActive, object: nil)
}
Swift 4.2
添加观察者-
NotificationCenter.default.addObserver(self, selector: #selector(handleEvent), name: UIApplication.didBecomeActiveNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIApplication.didBecomeActiveNotification, object: nil)
移除观察者-
NotificationCenter.default.addObserver(self, selector: #selector(handleEvent), name: UIApplication.didBecomeActiveNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIApplication.didBecomeActiveNotification, object: nil)
处理事件-
@objc func handleEvent() {
}
Swift 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
}
组合方式:
import Combine
var cancellables = Set<AnyCancellable>()
NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)
.sink { notification in
// do stuff
}.store(in: &cancellables)
导入联合收割机
var cancelables=Set()
NotificationCenter.default.publisher(适用于:UIApplication.didBecomeActiveNotification)
.sink{中的通知
//做事
}.store(在:&可取消项中)
Swift 5版本:
NotificationCenter.default.addObserver(self,
selector: #selector(loadData),
name: UIApplication.didBecomeActiveNotification,
object: nil)
在iOS 9及更高版本中删除观察器。如果您使用的是SwiftUI:
.onReceive(NotificationCenter.default.publisher(
for: UIApplication.didBecomeActiveNotification)) { _ in
print("DID BECOME ACTIVE")
}
)
斯威夫特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() {
}
对于Swift5 MacOS,您需要使用NSApplication而不是UIApplication
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(someMethod:)
name:UIApplicationDidBecomeActiveNotification object:nil];
NotificationCenter.default.addObserver(self,
selector: #selector(applicationDidBecomeActive),
name: (NSApplication.didBecomeActiveNotification),
object: nil)
}
杰出的没有想到使用NSNotificationCenter
。非常感谢。只是那行代码中的一个输入错误(缺少“名称”):[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(someMethod:)name:UIApplicationIDBecMeactiveNotification对象:nil];要添加到Reed的答案中,调用的方法(在本例中是someMethod)需要接受NSNotification参数。所以someMethod的方法签名应该是-(void)someMethod:(NSNotification*)notification{//Do somethine Here}@Aaron它可以,但这不是一个要求。不过,这是很好的洞察力。谢谢好极了在视图控制器/其他负责这些NSTimer的对象中,有多么好的方法可以使NSTimer实例失效/重新创建。爱死它了!在Swift中放置removeObserver
的最佳位置:deinit
方法。通常不建议在deinit中访问self;此时,self处于完全分配和解除分配之间。那么,您将从何处删除Observer?@Enricousatyo您可以忽略这一点,因为这是不正确的。重写deinit很好:“因为实例在调用其deinitializer之后才会解除分配,所以deinitializer可以访问其被调用的实例的所有属性,并可以基于这些属性修改其行为(例如查找需要关闭的文件的名称)。”调用Denit不合适。我在哪里调用它?@user8169082,您可以在需要开始接收通知的任何位置添加一个观察者。例如,您可以将其添加到viewDidLoad
或viewwilldisease:animated
。当您不再需要通知时,或者当您的观察者实例将在我使用的deinit methodswift 4.2中解除分配时,您可以删除观察者:NotificationCenter.default.addObserver(self,selector:#selector(ApplicationIDBecMeactive(notification:)),name:UIApplication.didBecomeActiveNotification,object:nil)#选择器
可以调用Swift 4中声明为@objc
属性的方法。使用removeObserver(self
是不正确的,因为在添加观察者时未分配self。您应该让观察者=NotificationCenter.default.addObserver
然后removeObserver(observer
谢谢@CodeBender我还不知道这个函数,它(最终)删除了@objc
。但是当我尝试它时,我在控制台中得到了一个警告(Xcode 11.3.1(11C504),Swift 13.3):无法结束BackgroundTask:不存在标识符为的后台任务。即使我将观察者保存在变量中作为NSObjectProtocol。不过,如果我使用@objc
变量,我也会收到警告。