【发布时间】: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