ios - 通过 Storyboard有效地传递 NSManagedObject

标签 ios iphone swift core-data uiviewcontroller

我的应用程序中有一个相当复杂的设置,涉及一个嵌入的 UISplitViewController,它提供一堆 UIViewControllers

我需要做的是通过嵌入的 UISplitViewController 传递一个 NSManagedObject,这样我就可以在我的单独详细信息“UIViewControllers”中访问它。

我已经附上了我的 Storyboard的图像和一些很棒的注释.... enter image description here

这是我目前的 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/

相关文章:

ios - 如何将路线时间显示为谷歌地图

ios - 如何通过 ImagePickerViewController 获取录制视频的 Assets url?

ios - PopoverPresentation Controller 未以左上箭头方向显示在屏幕中央

swift - 模块外部未检测到具有协议(protocol)一致性的扩展

objective-c - 如何创建水平 UIPickerView 样式控件?

ios - UITableView:如何将页脚附加到具有分组动态原型(prototype)单元格的部分?

ios - Cocos2D中如何保存和调用高分

ios - NSKeyedArchiver archivedDataWithRootObject 在传递图像模型数组时返回 nil

iphone - UIView TransitionFromView 示例?

ios - 从数组中的动态坐标对 Google 标记移动进行动画处理