有效地通过情节提要传递NSManagedObject [英] Passing an NSManagedObject Effectively through Storyboard
问题描述
我的应用程序中有一个非常复杂的设置,其中涉及一个嵌入式 UISplitViewController
,它可以提供一堆 UIViewControllers
。
我需要做的是通过嵌入式 UISplitViewController传递
NSManagedObject
code>,这样我就可以在单独的详细信息中访问它了 UIViewControllers
。
我已经附加了图片我的情节提要中带有一些很棒的注释。...
这是我目前的prepareForSegue函数,位于我已经有我的NSManagedObject的View Controller,我希望将其传递给第一个SurveyViewController,以便它可以继续传递:
覆盖func prepareForSegue(segue:UIStoryboardSegue,发送者:AnyObject?){
println(发件人是\(发件人))
如果segue.identifier == SurveyIdentifier {
// //使导航:UINavigationController = segue.destinationViewController为! UINavigationController
// //让itemVC:BACUploaderViewController = nav.topViewController为! BACUploaderViewController
让SurveyViewController:SurveyViewController = segue.destinationViewController为! SurveyViewController
println( survey view controller:\(surveyViewController))
//如果destination = segue.destinationViewController为? MasterViewController {
//
// destination.workItem =发件人为?工作
//}
}
}
我相当确定我必须通过深入查看层次结构从这里访问我的 MasterViewController
,但是不确定如何有效地实现这一点? / p>
在侧面说明中,segue确实起作用,向我显示了正确的视图,但是它将视图两次推到屏幕上,我不确定为什么,我
可以上传此内容的gif文件吗? 通过视图控制器的层次结构来获取对故事板中标题为 Sections的表视图控制器的引用。
来自 prepareForSegue
在您的 SurveyViewController
中:
//目的地嵌入 segue的内容将是拆分视图控制器
让splitVC:UISplitViewController = segue.destinationViewController如! UISplitViewController
//在UISplitViewController中,viewControllers [0]表示主视图控制器
//在您的情况下是UINavigationController ...
让navVC:UINavigationController = splitVC.viewControllers [0作为! UINavigationController
//在UINavigationController中,topViewController表示可见的VC
//在您的情况下,这是Sections表视图控制器...
让sectionVC:SectionsViewController = navVC.topViewController为! SectionsViewController
//(用正确的类名替换 SectionsViewController)
sectionVC.object = yourNSManagedObject
这会将对象传递给Sections视图控制器。您可以在Sections视图控制器的 prepareForSegue
中将对象传递给最终的VC(坏孩子!)。您不能更早完成操作,因为在此之前它们不会被实例化。
关于为什么视图可能两次被推到屏幕上的原因,我唯一的猜测是当segue也直接链接到原型单元时,请在表视图的 didSelectRowAtIndexPath
中使用 performSegueWithIdentifier
。 / p>
I have quite a complicated setup in my app that involves an embedded UISplitViewController
which feeds a bunch of UIViewControllers
.
What I am needing to do is pass an NSManagedObject
through the embedded UISplitViewController
so I can access it in my separate detail' UIViewControllers
.
I have attached an image of my storyboard with some awesome annotations....
This is my prepareForSegue function at the minute, this is in the View Controller where I already have my NSManagedObject, I am looking to pass it to the first SurveyViewController so it can get passed on:
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
// }
}
}
I am fairly sure that I have to access my MasterViewController
from here by drilling down through the View Hierarchy, but not sure on how to achieve this effectively?
On a side note, the segue does work, I am presented with the correct views, but it pushes the view onto the screen twice and I'm not sure why, I can upload a gif of this if seeing what I am talking about in action might make more sense?
You can "reach down" through the hierarchy of view controllers, to get a reference to the table view controller titled "Sections" in the storyboard.
From prepareForSegue
in your SurveyViewController
:
// 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
This passes the object to the Sections view controller. You can pass the object to the final VCs (the bad boys!) in the prepareForSegue
of the Sections view controller. You can't do it earlier, because they are not instantiated before then.
As to why the view might be pushed onto screen twice, my only guess is that you might be using performSegueWithIdentifier
from within didSelectRowAtIndexPath
of a table view, when the segue is also linked directly to the prototype cell.
这篇关于有效地通过情节提要传递NSManagedObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!