【问题标题】:performSegueWithIdentifier not working when being called from new instance of viewController in Swift从 Swift 中的 viewController 的新实例调用时 performSegueWithIdentifier 不起作用
【发布时间】:2016-07-15 17:34:16
【问题描述】:

我的使命

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

当前代码

PushNotification.swift - 一个具有静态函数的类,用于处理接收推送通知时的行为

__getNavigationController 根据 Tab-and viewIndex 从 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 在用户单击通知时被调用,并且该方法调用 __getApplication 以从数据库中获取应用程序,并在推送通知中收到 objectId,然后实例化 GroupTableViewController 以执行 segue SingleApplicationViewController。

(TabbarController -> Navigation Controller -> GroupTableViewController -> SingleApplicationViewController)

有点奇怪的是,当我将 tabIndex 设置为 0 并将 viewIndex 设置为 1 时。然而,GroupView 在第二个选项卡(选项卡 1)上,视图控制器应该是第一个(0)。但是当我将它们设置为相应的数字时,我收到 nil 并且应用程序崩溃。

我读到你会在执行_ = groupTableViewController.view 时强制加载视图控制器,而它实际上是这样做的。当它被调用时, viewDidLoad -function 被调用。

/************** 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 之前的中间步骤

} else if segue.identifier == "GroupTableToApplicationToDetailApplication" {
                let navC = segue.destinationViewController as! UINavigationController
                let controller = navC.topViewController as! ApplicationViewController
                controller.performSegueWithIdentifier("ApplicationsToSingleApplicationSegue", sender: sender as! Application)
            }

那么,什么不工作?

好吧,GroupTableViewController 中的 prepareForSegue 没有被调用。我在我的 TimeLineViewController 上使用相同的代码结构,并且几乎完全相同的代码,当获得另一个推送通知时,它工作得很好。在这种情况下,我使用 tabIndex 0 和 viewIndex 0 来获取正确的 NavigationController。

请,任何想法和/或建议都非常受欢迎!

【问题讨论】:

  • 你的结构不是很清楚,如果你需要一些帮助,你可以画出结构或者贴一张关于你的故事板的图片,这样在特定的时刻很难理解你叫什么..跨度>
  • 你的问题中有很多代码,我不相信这一切都是必要的。请发布您的问题的Minimal, Complete, and Verifiable example
  • 我相信这一切都是必要的。我不确定真正的问题出在哪里,因此我必须包含与执行此 segue 有关的所有代码。虽然,我同意@AlessandroOrnano,但可能不清楚在哪里发生了什么。我会先尝试解决方案,然后再带一张照片回来。谢谢

标签: ios swift view controller push-notification


【解决方案1】:

以下方法有变化..

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.selectedViewController给你UINavigationController

这里你得到了导航控制器对象。在你的 applicationClicked 方法中,导航对象可能是 nil,所以它不能执行进一步的执行代码。

检查以下方法。

/************** 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
            }
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-20
    • 2012-05-01
    • 2014-09-18
    • 2016-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多