使用#selector 可以替代 UIApplication.didBecomeActiveNotification 吗?
Is there an alternative to UIApplication.didBecomeActiveNotification with #selector?
我需要从 tabbar controller
执行 segue
到 modal
。这是行不通的,因为有一个起始 tab
覆盖了模式转场(我认为。模式不会显示)。而且我无法在开始 tab
中执行 segue
因为不同的产品 targets
有不同的开始 tabs
并且在多个地方使用相同的代码会很混乱。
反正我用
解决了
NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)
@objc func applicationDidBecomeActive(notification: Notification) {
performSegue(withIdentifier: "Segue", sender: nil)
}
效果很好。但是,我不想使用这个旧的 @objc
代码。难道没有一种现代快速的方式来做到这一点吗?我可以使用 UIApplication.didBecomeActiveNotification
,但我想避免使用 #selector
和 @objc
。
如果您的目标是 iOS 13 岁以上,则可以使用 Combine
观察 NotificationCenter
通知,而无需使用选择器。 NotificationCenter
的publisher(for:)
函数可以用来做这个
var subscriptions = Set<AnyCancellable>()
let notificationPublisher = NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)
notificationPublisher.sink { _ in performSegue(withIdentifier: "Segue", sender: nil) }.store(in: &subscriptions)
我需要从 tabbar controller
执行 segue
到 modal
。这是行不通的,因为有一个起始 tab
覆盖了模式转场(我认为。模式不会显示)。而且我无法在开始 tab
中执行 segue
因为不同的产品 targets
有不同的开始 tabs
并且在多个地方使用相同的代码会很混乱。
反正我用
解决了NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)
@objc func applicationDidBecomeActive(notification: Notification) {
performSegue(withIdentifier: "Segue", sender: nil)
}
效果很好。但是,我不想使用这个旧的 @objc
代码。难道没有一种现代快速的方式来做到这一点吗?我可以使用 UIApplication.didBecomeActiveNotification
,但我想避免使用 #selector
和 @objc
。
如果您的目标是 iOS 13 岁以上,则可以使用 Combine
观察 NotificationCenter
通知,而无需使用选择器。 NotificationCenter
的publisher(for:)
函数可以用来做这个
var subscriptions = Set<AnyCancellable>()
let notificationPublisher = NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)
notificationPublisher.sink { _ in performSegue(withIdentifier: "Segue", sender: nil) }.store(in: &subscriptions)