简体   繁体   English

从AppDelegate.swift执行Segue

[英]Performing Segue from AppDelegate.swift

I am trying to perform a segue after getting a notification. 我在收到通知后尝试执行segue。

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? 这样我就可以从第二种观点出发,就好像我从第一种观点出发一样?

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. 另外,更健壮的方法是从AppDelegate广播NSNotification,并让您的视图控制器监听特定的通知。

My solution: add public variable in AppDelegate (etc. flag =1) 我的解决方案:在AppDelegate中添加公共变量(等标志= 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. 并检查rootviewcontroller-> ViewDidLoad上的标志。 if it's true ? 如果是真的? call your spesific segue 打电话给你特别的segue

    override func viewDidLoad() {
    super.viewDidLoad()
    if (flag == 1) {
        self.performSegue(withIdentifier: "roottanBildirimSegue", sender: self)
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM