问题
I am trying to perform a segue after getting a notification.
Here is what I have in my app delegate.
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
let viewController = self.window!.rootViewController!.storyboard!.instantiateViewControllerWithIdentifier("SecondViewController")
self.window?.rootViewController = viewController
}
This takes me directly to the view I would like it to go to, however the navbar and tab bar are hidden. From the app delegate, how do I perform a segue from one view to another? So that I can reach by second view as if I had pushed from my first?
回答1:
Did you try with:
viewController.performSegueWithIdentifier("my_segue_identifier", sender: nil)
Also the more robust way is to broadcast a NSNotification from your AppDelegate and have your viewcontroller listen for a specific notification.
回答2:
My solution: add public variable in AppDelegate (etc. flag =1)
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("User Info = ",response.notification.request.content.userInfo)
flag = 1; <---
completionHandler()
}
and check the flag on rootviewcontroller-> ViewDidLoad. if it's true ? call your spesific segue
override func viewDidLoad() {
super.viewDidLoad()
if (flag == 1) {
self.performSegue(withIdentifier: "roottanBildirimSegue", sender: self)
}
来源:https://stackoverflow.com/questions/33983116/performing-segue-from-appdelegate-swift