从本地通知代码转到视图控制器不起作用

问题描述 投票:0回答:1

我已经尝试过在这里找到的一百万种不同的东西。这是我没有收到任何错误的消息,我在其中添加了一条print语句,以确保此func正在运行并且确实在运行。我在做什么错?

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {


        let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)

        let nextViewController = storyBoard.instantiateViewController(withIdentifier: "calendarVC") as! CalendarVC
        self.window?.rootViewController?.present(nextViewController, animated: true, completion: nil)



    completionHandler()
}

我也尝试过此代码,当我点击通知时什么也没有发生:

        if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "calendarVC") as? CalendarVC {
        if let window = self.window, let rootViewController = window.rootViewController {
            var currentController = rootViewController
            while let presentedController = currentController.presentedViewController {
                currentController = presentedController
            }
            currentController.present(controller, animated: true, completion: nil)
        }

我在这里做错了什么?我不在乎我使用哪个代码,我只希望用户能够点击通知并将其带到我的CalendarVC

编辑:哦,是的,我确保已设置好情节提要编辑2:只是要弄清楚我的问题是什么。我猜代码正在运行,因为代码中的print语句正在运行,但是它什么也没做。不带我去CalendarVC

ios swift xcode localnotification
1个回答
0
投票

尝试最后一行:

代替

self.window?.rootViewController?.present(nextViewController, animated: true, completion: nil)

执行此操作:

self.window?.rootViewController = nextViewController

© www.soinside.com 2019 - 2024. All rights reserved.