I need to perform a segue
to a modal
from my tabbar controller
. This won't work since there's a starting tab
that overrides the modal segue (I think. The modal won't show). And I can't perform the segue
in the starting tab
since different product targets
have different starting tabs
and it would be messy having the same code in multiple places.
Anyway, I solved it by using
NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)
@objc func applicationDidBecomeActive(notification: Notification) {
performSegue(withIdentifier: "Segue", sender: nil)
}
It works great. But, I don't want to use this old @objc
code. Isn't there a mode modern swifty way to do this? I'm fine using UIApplication.didBecomeActiveNotification
, but I want to get away from using the #selector
and @objc
.
If you are targeting iOS 13+, you can use Combine
to observe NotificationCenter
notifications without having to use selectors. The publisher(for:)
function of NotificationCenter
can be used for this.
var subscriptions = Set<AnyCancellable>()
let notificationPublisher = NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)
notificationPublisher.sink { _ in performSegue(withIdentifier: "Segue", sender: nil) }.store(in: &subscriptions)