在我的故事板中,我有一个View Controller,我还有一个导航控制器和另一个名为HistoryController的View Controller.导航控制器和HistroyController有一个关系“根视图控制器”
我的第一个视图控制器上有一个按钮,该按钮有一个推送到导航控制器的按钮.
我在第一个视图控制器中有这个代码来准备segue:
@H_403_5@override func prepare(for segue: UIStoryboardSegue,sender: Any?) { if let viewController = segue.destination as? HistoryController { viewController.detailItem = barcodeInt as AnyObject } }我的问题是当我运行我的代码并按下我的第一个控制器中的按钮时,我收到此错误:
@H_403_5@Could not find a navigation controller for segue 'HistorySegue'. Push segues can only be used when the source controller is managed by an instance of UINavigationController.'我尝试了以下内容
@H_403_5@override func prepare(for segue: UIStoryboardSegue,sender: Any?) { if let navVC = segue.destinationViewController as? UINavigationController{ if let historyVC = navVC.viewControllers[0] as? HistoryController{ historyVC.detailItem = barcodeInt as AnyObject } } }我也试过了
@H_403_5@override func prepare(for segue: UIStoryboardSegue,sender: Any?) { if let nav = segue.destination as? UINavigationController { if let vc = nav.visibleViewController as? HistoryController { vc.detailItem = barcodeInt as AnyObject } } }我仍然得到同样的错误:
@H_403_5@Could not find a navigation controller for segue 'HistorySegue'. Push segues can only be used when the source controller is managed by an instance of UINavigationController.'
您需要将源视图控制器嵌入到导航控制器中,或将segue类型从推送更改为其他内容.此外,尝试将目标控制器转换为UINavigationController以防止在初始控制器之后出现另一个错误(如果已修复):
@H_403_5@override func prepare(for segue: UIStoryboardSegue,sender: Any?) {
if let navVC = segue.destinationViewController as? UINavigationController{
if let historyVC = navVC.viewControllers[0] as? HistoryController{
historyVC.detailItem = barcodeInt as AnyObject
}
}
}
原文链接:https://www.f2er.com/swift/319261.html看看它是否适合你.
编辑:更新了上面的代码.