Ios 在Swift中从viewController的新实例调用时,PerformsgueWithIdentifier不工作 我的使命

Ios 在Swift中从viewController的新实例调用时,PerformsgueWithIdentifier不工作 我的使命,ios,swift,view,controller,push-notification,Ios,Swift,View,Controller,Push Notification,当应用程序收到通知并且用户点击通知时,我想将用户重定向到正确的视图。在我的例子中,是SingleApplicationViewController internal static func __getNavigationController(tabIndex: Int, viewIndex: Int) -> UINavigationController { let appDelegate = UIApplication.sharedApplication().delegat

当应用程序收到通知并且用户点击通知时,我想将用户重定向到正确的视图。在我的例子中,是SingleApplicationViewController

internal static func __getNavigationController(tabIndex: Int, viewIndex: Int) -> UINavigationController {
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

        let window:UIWindow? = (UIApplication.sharedApplication().delegate?.window)!
        let storyBoard = UIStoryboard(name: "Main", bundle: nil)
        let viewController = storyBoard.instantiateViewControllerWithIdentifier("MainEntry")
        window?.rootViewController = viewController

        let rootViewController = appDelegate.window!.rootViewController as! UITabBarController
        rootViewController.selectedIndex = tabIndex
        let nav = rootViewController.viewControllers![viewIndex] as! UINavigationController

        return nav
    }
当前代码 PushNotification.swift-一个具有静态函数的类,用于在接收推送通知时处理行为

__getNavigationController根据TabBarController中的选项卡和视图索引返回特定的NavigationController

internal static func __getNavigationController(tabIndex: Int, viewIndex: Int) -> UINavigationController {
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

        let window:UIWindow? = (UIApplication.sharedApplication().delegate?.window)!
        let storyBoard = UIStoryboard(name: "Main", bundle: nil)
        let viewController = storyBoard.instantiateViewControllerWithIdentifier("MainEntry")
        window?.rootViewController = viewController

        let rootViewController = appDelegate.window!.rootViewController as! UITabBarController
        rootViewController.selectedIndex = tabIndex
        let nav = rootViewController.viewControllers![viewIndex] as! UINavigationController

        return nav
    }
当用户单击通知时,将调用applicationClicked,该方法调用uu getApplication从数据库中获取应用程序,并在推送通知中接收objectId,然后实例化GroupTableViewController以执行到SingleApplicationViewController的转换

(TabbarController -> Navigation Controller -> GroupTableViewController -> SingleApplicationViewController)
有点奇怪的是,我将tabIndex设置为0并将viewIndex设置为1。但是,GroupView位于第二个选项卡(选项卡1)上,视图控制器应位于第一个选项卡(0)上。但是,当我将它们设置为相应的数字时,我会收到nil,应用程序就会崩溃

我了解到,在执行
\uu=groupTableViewController.view时,您将强制加载视图控制器,而实际上它是这样做的。调用此函数时,将调用viewDidLoad-函数

/************** APPLICATION ***************/
    static func applicationClicked(objectId: String) {
        __getApplication(objectId) { (application, error) in
            if application != nil && error == nil {
                let nav = __getNavigationController(0, viewIndex: 1)
                let groupTableViewController = nav.viewControllers.first as! GroupsTableViewController
                _ = groupTableViewController.view

                groupTableViewController.performSegueWithIdentifier("GroupTableToApplicationToDetailApplication", sender: application!)
            } else {
                // Hanlde error
            }
        }
    }
GroupTableViewController.prepareForSegue()

在这里,我创建了ApplicationTableViewController的一个新实例,这是进入SingleApplicationViewController之前的中间步骤

那么,什么不起作用? 嗯,没有调用GroupTableViewController中的prepareForSegue。当我收到另一个推送通知时,我在TimeLineViewController上使用了相同的代码结构,几乎完全相同的代码,它工作得非常完美。在这种情况下,我使用tabIndex 0和viewIndex 0来获取正确的NavigationController

internal static func __getNavigationController(tabIndex: Int, viewIndex: Int) -> UINavigationController {
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

        let window:UIWindow? = (UIApplication.sharedApplication().delegate?.window)!
        let storyBoard = UIStoryboard(name: "Main", bundle: nil)
        let viewController = storyBoard.instantiateViewControllerWithIdentifier("MainEntry")
        window?.rootViewController = viewController

        let rootViewController = appDelegate.window!.rootViewController as! UITabBarController
        rootViewController.selectedIndex = tabIndex
        let nav = rootViewController.viewControllers![viewIndex] as! UINavigationController

        return nav
    }

欢迎您提出任何想法和/或建议

以下方法有变化

internal static func __getNavigationController(tabIndex: Int) -> UINavigationController {
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

        let window:UIWindow? = (UIApplication.sharedApplication().delegate?.window)!
        let storyBoard = UIStoryboard(name: "Main", bundle: nil)
        let viewController = storyBoard.instantiateViewControllerWithIdentifier("MainEntry")
        window?.rootViewController = viewController

        let rootViewController = appDelegate.window!.rootViewController as! UITabBarController
        rootViewController.selectedIndex = tabIndex
        let nav = rootViewController.selectedViewController as! UINavigationController  //This will return navigation controller..
                        //No need of viewIndex..

        return nav
    }
你已经写信了

let nav = rootViewController.viewControllers![viewIndex] as! UINavigationController 
更改为
rootViewController。选择ViewController
可为您提供
UINavigationController

这里您可以看到navigavtion控制器对象。在应用程序中,单击的方法nav对象可能为零,因此它无法执行进一步的performsegue代码

检查以下方法

/************** APPLICATION ***************/
    static func applicationClicked(objectId: String) {
        __getApplication(objectId) { (application, error) in
            if application != nil && error == nil {
                let nav = __getNavigationController(0)//0 is your tab index..if you want 1 then replace it with  1
                let groupTableViewController = nav.viewControllers.first as! GroupsTableViewController  //Rootview controller of Nav Controller
               groupTableViewController.performSegueWithIdentifier("GroupTableToApplicationToDetailApplication", sender: application!) //Perform seque from Root VC...
            } else {
                // Hanlde error
            }
        }
    }

你的结构不清楚,如果你想得到一些帮助,你可以画出结构或者贴一张关于你故事板的图片,就像这样,很难理解你在特定时刻所说的。你的问题中有很多代码,我不相信这是必要的。请把你的问题贴出来。不过我相信这一切都是必要的。我不确定真正的问题在哪里,因此我必须包括所有与执行此序列有关的代码。虽然我同意@AlessandroOrnano的观点,但可能不清楚在哪里发生了什么。我会先试试这个解决方案,然后再拿一张照片回来。非常感谢。