Performing Segue from AppDelegate.swift

情到浓时终转凉″ 提交于 2020-01-16 05:38:14

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!