通过故事板有效地传递 NSManagedObject
Passing an NSManagedObject Effectively through Storyboard
我的应用程序中有一个相当复杂的设置,其中涉及一个嵌入式 UISplitViewController
,它提供了一堆 UIViewControllers
。
我需要做的是通过嵌入式 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
您可以 "reach down" 通过视图控制器的层次结构,以获取故事板中标题为 "Sections" 的 table 视图控制器的引用。
来自 prepareForSegue
在您的 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
这会将 object 传递给 Sections 视图控制器。您可以在 Sections 视图控制器的 prepareForSegue
中将 object 传递给最终的 VC(坏男孩!)。你不能早点做,因为他们在那之前没有实例化。
至于为什么视图可能会被推送到屏幕上两次,我唯一的猜测是您可能在 table 视图的 didSelectRowAtIndexPath
中使用 performSegueWithIdentifier
,当 segue也直接链接到原型单元格。
我的应用程序中有一个相当复杂的设置,其中涉及一个嵌入式 UISplitViewController
,它提供了一堆 UIViewControllers
。
我需要做的是通过嵌入式 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
您可以 "reach down" 通过视图控制器的层次结构,以获取故事板中标题为 "Sections" 的 table 视图控制器的引用。
来自 prepareForSegue
在您的 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
这会将 object 传递给 Sections 视图控制器。您可以在 Sections 视图控制器的 prepareForSegue
中将 object 传递给最终的 VC(坏男孩!)。你不能早点做,因为他们在那之前没有实例化。
至于为什么视图可能会被推送到屏幕上两次,我唯一的猜测是您可能在 table 视图的 didSelectRowAtIndexPath
中使用 performSegueWithIdentifier
,当 segue也直接链接到原型单元格。