将数据从Detail View Controller传递到另一个表View

时间:2017-04-24 17:59:35

标签: ios swift core-data

我有两个表视图控制器,数据来自端点。在表视图1的详细视图中,我想将选择的对象传递给表视图2,试图使用准备segue但不成功。这是我的代码

第一张表视图

class ListContentsViewController: BaseMainList {


    lazy var concreteFetchedResultsController: NSFetchedResultsController = { [unowned self] in
        let occurredAtSort = NSSortDescriptor(key: "occuredAt", ascending: false)
        let identifierSort = NSSortDescriptor(key: "identifier", ascending: false)

        let request = NSFetchRequest(entityName: "Contents")
        request.sortDescriptors = [occurredAtSort, identifierSort]


        let fetch = NSFetchedResultsController(fetchRequest: request, managedObjectContext: NSManagedObjectContext.MR_defaultContext(),
                                               sectionNameKeyPath: nil, cacheName: nil)
        fetch.delegate = self
        do {
            try fetch.performFetch()
        } catch _ {
        }

        return fetch
        }()

    override var fetchedResultsController: NSFetchedResultsController {
        get {
            return concreteFetchedResultsController
        }
        set {
            self.fetchedResultsController = newValue
        }


    }

       var selectedContents: Selected?

    // MARK: - View Life Cycle

    override func viewDidLoad() {
        super.viewDidLoad()





    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)


    }






    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if let segueId = segue.identifier {
            switch segueId {
            case "ContentDetailsSegue":
                if let cell = sender as? UITableViewCell, indexPath = tableView.indexPathForCell(cell) {
                    tableView.deselectRowAtIndexPath(indexPath, animated: true)

                    let object = fetchedResultsController.objectAtIndexPath(indexPath) as! Selected
                    let controller = segue.destinationViewController as! ContentsDetailsViewController
                    controller. selectedContents = object

                }

                    default:
                    ()
                }

        }
    }
}

内容详细信息视图控制器

  var selectedContents: Selected?

override func viewDidLoad() {
        super.viewDidLoad()



        self.automaticallyAdjustsScrollViewInsets = false
        self.navigationController!.navigationBar.translucent = true



        mapView.delegate = self

        loadContents()

    }


    // MARK: - Load 

    func load() {

   if let content = selectedContents {
            labelt.text = content.name

          labely.text = content.email

           labelu.text = content.number


}

  @IBAction func ignore(sender: UIBarButtonItem) {

        if let content = self. selectedContents {

            ContentServices.ignoreWithID(andContentID: content.identifier, withCompletion: { (error) in


                let alert = UIAlertController(title: NSLocalizedString("", comment: ""), message: NSLocalizedString("Success", comment: ""), preferredStyle: UIAlertControllerStyle.Alert)
                alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
                self.presentViewController(alert, animated: true, completion: nil)


                self.ignoreBt.enabled = false


                return
            })

        }

    }

预计点击按钮后,它会将在表格视图1中选择的对象传递到表格视图2.我该如何处理?

0 个答案:

没有答案