我的应用程序中有一个相当复杂的设置,涉及一个嵌入的 UISplitViewController
,它提供一堆 UIViewControllers
。
我需要做的是通过嵌入的 UISplitViewController
传递一个 NSManagedObject
,这样我就可以在我的单独详细信息“UIViewControllers
”中访问它。
我已经附上了我的 Storyboard的图像和一些很棒的注释....
这是我目前的 prepareForSegue 函数,它位于我已经拥有 NSManagedObject 的 View Controller 中,我希望将它传递给第一个 SurveyViewController,以便它可以继续传递:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
println("Sender is \(sender)")
if segue.identifier == SurveyIdentifier {
// let nav : UINavigationController = segue.destinationViewController as! UINavigationController
// let itemVC: BACUploaderViewController = nav.topViewController as! BACUploaderViewController
let surveyViewController: SurveyViewController = segue.destinationViewController as! SurveyViewController
println("survey view controller: \(surveyViewController)")
// if let destination = segue.destinationViewController as? MasterViewController {
//
// destination.workItem = sender as? Work
// }
}
}
我相当确定我必须通过向下钻取 View 层次结构从此处访问我的 MasterViewController
,但不确定如何有效地实现这一点?
附带说明,segue 确实有效,我看到了正确的 View ,但它会将 View 推到屏幕上两次,我不确定为什么,如果看到我的情况,我可以上传一个 gif谈论行动可能更有意义?
最佳答案
您可以通过 View Controller 的层次结构“向下延伸”,以获取对 Storyboard中标题为“Sections”的 TableView Controller 的引用。
来自 SurveyViewController
中的 prepareForSegue
:
// the destination of the "embed" segue will be the split view controller
let splitVC : UISplitViewController = segue.destinationViewController as! UISplitViewController
// in a UISplitViewController, viewControllers[0] represents the Master view controller
// in your case that is a UINavigationController...
let navVC: UINavigationController = splitVC.viewControllers[0] as! UINavigationController
// In a UINavigationController, topViewController represents the visible VC
// In your case that's the Sections table view controller...
let sectionsVC : SectionsViewController = navVC.topViewController as! SectionsViewController
// (replace "SectionsViewController" with the correct class name)
sectionsVC.object = yourNSManagedObject
这会将对象传递给 Sections View Controller 。您可以在 Sections View Controller 的 prepareForSegue
中将对象传递给最终的 VC(坏男孩!)。您不能更早执行此操作,因为它们在此之前未实例化。
至于为什么 View 可能被推送到屏幕上两次,我唯一的猜测是您可能在 TableView 的 didSelectRowAtIndexPath
中使用 performSegueWithIdentifier
,当 segue也直接链接到原型(prototype)单元格。
关于ios - 通过 Storyboard有效地传递 NSManagedObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32333689/