當調用performSegue
時,我的應用並不總是延續到下一個視圖控制器。但是,它總是立即執行prepare(for:sender)
。performSegue並不總是過渡,但準備(對於發件人)始終被稱爲
- 有什麼理由存在的
performSegue
不不必硬錯誤的工作(並仍在執行prepareForSegue
)? - 是否有一個狀態,源視圖控制器應該在我需要檢查?
- 如果我放了10秒的延遲(通過
asyncAfter
),發生了繼續。
方案
我試圖重新宗旨,爲新功能的工作賽格瑞(集成Spotlight搜索)的過渡不會在我的iPhone發生,如果應用程序留在細節視圖。 (該應用程序具有UISplitViewController作爲其根視圖控制器。)
然而,代碼工作時
- 我在我的iPad測試如預期 - 即當正被示出主機和詳細視圖。
- 當我離開應用程序在主視圖上時,我在我的iPhone上進行測試。
當我從詳情屏幕測試我的iPhone {見下文代碼}:
- 細節屏幕消失(每
popToRootViewController
) - 主視圖被示出,與所述正確的行突出顯示(包括NSLog說「進入主」)
- 函數執行,包括
prepare(for:sender)
,但從不顯示詳細視圖。
在這一點上,我可以點擊相應的行和SEGUE發生如預期 - 這是tableView(:didSelectRowAt:)
電話performSegue
。
更詳細
夫特3.0 &的iOS 10
該應用具有UISplitViewController作爲其RootViewController的。主視圖是帶有配方列表的tableView。詳細視圖是列出所選配方成分的tableView。 Segue從配方行選擇轉換到其配料表。
我正在添加一項新功能:將食譜放入Spotlight中,以便用戶可以搜索它們,並在選定時從主視圖中選擇配方,並在細節視圖中顯示其配料。
現在我想實現通過的appDelegate的application(:continue:restorationHandler)
Spotlight搜索整合:
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
guard userActivity.activityType == CSSearchableItemActionType,
let userInfo = userActivity.userInfo,
let id = userInfo[CSSearchableItemActivityIdentifier] as? String
else {
return false
}
// The id is a unique identifier for recipe.
if let inputRecipe = Recipe.getBy(uuid: id, moc: self.managedObjectContext) {
let root = self.window?.rootViewController as! UISplitViewController
let nav = root.viewControllers[0] as! UINavigationController
// If this is a navigation controller, then detail is being shown
if (nav.topViewController as? UINavigationController) != nil {
NSLog("Should be a detail view")
nav.popToRootViewController(animated: true)
}
if let masterVC = nav.topViewController as? RecipeMasterVC {
NSLog("Into the master")
masterVC.selectTableRowAt(recipe: inputRecipe)
}
}
return true
}
內RecipeMasterVC,我加入這個代碼Segue公司
func selectTableRowAt(recipe: Recipe){
let indexPath = self.fetchedResultsController.indexPath(forObject: recipe)
self.tableView.selectRow(at: indexPath, animated: true, scrollPosition: .top)
DispatchQueue.main.async {
self.performSegue(withIdentifier: seguesEnum.RecipeDetail.rawValue, sender: recipe)
}
}
主視圖控制器可能尚未加載。視圖控制器完全可能存在 - 即init()已被調用,以便分割視圖控制器引用它,但viewDidLoad()尚未被調用。嘗試在viewDidLoad()中添加一些調試代碼,或者檢查selectTableRow中的isViewLoaded方法以在主視圖實際加載時進行跟蹤。 – Dale
@Dale - 在Dispatch {performSegue}之前,'self.isViewLoaded'的值爲true。 – AgRizzo
我在我自己的應用程序中有類似的情況,沒有使用segue,但仍然使用拆分視圖。當我檢查代碼時,我發現我必須添加200ms的延遲才能使其工作。從來沒有得到的根本原因,但沒有延遲,即使有調度異步不起作用,如果詳細信息屏幕已被顯示 – Dale