Ios 通过故事板有效地传递NSManagedObject
我的应用程序中有一个相当复杂的设置,其中包括一个嵌入式Ios 通过故事板有效地传递NSManagedObject,ios,iphone,swift,core-data,uiviewcontroller,Ios,Iphone,Swift,Core Data,Uiviewcontroller,我的应用程序中有一个相当复杂的设置,其中包括一个嵌入式UISplitViewController,它为一组uiviewcontroller提供数据 我需要做的是通过嵌入的UISplitViewController传递一个NSManagedObject,这样我就可以在我单独的详细信息中访问它了'UIViewControllers 我已经附上了我的故事板与一些可怕的注释图像 这是我目前的prepareForSegue函数,它在视图控制器中,我已经有了NSManagedObject,我希望将它传递给第
UISplitViewController
,它为一组uiviewcontroller
提供数据
我需要做的是通过嵌入的UISplitViewController
传递一个NSManagedObject
,这样我就可以在我单独的详细信息中访问它了'UIViewControllers
我已经附上了我的故事板与一些可怕的注释图像
这是我目前的prepareForSegue函数,它在视图控制器中,我已经有了NSManagedObject,我希望将它传递给第一个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
// }
}
}
我相当确定我必须通过深入查看视图层次结构从这里访问我的MasterViewController
,但不确定如何有效地实现这一点
顺便说一句,segue确实有效,我看到了正确的视图,但它将视图推到屏幕上两次,我不知道为什么,如果看到我正在谈论的内容可能更有意义的话,我可以上传一个gif
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
***********************
surveyViewController.managedObject = self.managedObjectContext
************************
// if let destination = segue.destinationViewController as? MasterViewController {
//
// destination.workItem = sender as? Work
// }
}
}
您可以在SurveyViewController
中有一个名为managedObject
的可选对象,然后将managedObjectContext
对象从MasterViewController
注入SurveyViewController
。编写此代码段时,请记住您位于MasterViewController
中,您可以通过视图控制器的层次结构“向下延伸”,以获取对序列图像板中标题为“节”的表视图控制器的引用
从您的调查视图控制器中的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的prepareForSegue
中将对象传递给最终的VCs(坏小子!)。您不能更早地执行此操作,因为在此之前它们没有实例化
至于为什么视图可能会被推到屏幕上两次,我唯一的猜测是,当segue也直接链接到原型单元格时,您可能会在表视图的didselectrowatinexpath
中使用performsguewithidentifier
,您应该通过依赖项注入将您的NSManagedObject
传递给ViewController。这听起来是一个更好的解决方案,我该如何实现这一点呢?这与在目标视图类中声明NSManagedObject,然后将传递的对象分配给它不一样吗,例如,在我的目的地中,我有var对象:NSManagedObjectSubClass?然后在segue中have destination.object=NSManagedObjectSubClass。。很像我的评论?谢谢你的回复!嗯,问题是我遇到了这个错误,无法将“my.SurveyViewController”(0x179058)类型的值强制转换为“UISplitViewController”(0x36c35244)。我知道为什么,这是因为我有一个surveyViewController作为视图,该视图包含一个容器,其中包含UISplitViewController,如果您愿意的话?因此,我的segue.destination是SurveyViewController而不是UISplitViewController,我如何访问UISplitViewController?完全不理我,我意识到我必须更进一步,确保我的NSManageObject wsa被传递到SurveyViewController,我错过了你说的地方在您的调查问卷中的prepareForSegue视图控制器中:“我无法对此进行足够的表决,问题已解决!!:):)