[英]performSegueWithIdentifier not working when being called from new instance of viewController in Swift
當應用程序收到通知並且用戶點擊通知時,我想將用戶重定向到正確的視圖。 就我而言, SingleApplicationViewController 。
PushNotification.swift - 具有靜態函數的類,用於在接收推送通知時處理行為
__getNavigationController基於tabBarController的tab-and viewIndex返回特定的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
}
當在通知用戶點擊該方法調用__getApplication與推送通知接收到的ObjectID來從DB中的應用程序,然后實例化一個GroupTableViewController執行SEGUE到SingleApplicationViewController的applicationClicked正在被呼叫。
(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。
請,任何想法和/或建議都非常歡迎!
以下方法有變化..
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
。
在這里你得到navigavtion控制器對象..在你的applicationClicked方法導航對象可能是零,所以它不能執行進一步執行代碼。
檢查以下方法。
/************** 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
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.