将数据与 segue 一起传递

Passing data along with segues

问题在于:我试图将一些数据从一个 ViewController 传递到另一个 InfoViewController。它看起来像这样:

运行,应用程序,它看起来像这样:

除了这两个视图控制器,我在这个应用程序中还有一些其他视图控制器,它们的 Segues 工作正常,但是,当我点击单元格时,它确实会转到信息ViewController,但信息没有得到更新。

这是我的代码:

import UIKit import CoreData

protocol sendNameDelegate {
    func sendName(name: String) }

protocol sendImageDelegate {
    func sendImage(image: UIImage) }

class MainViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, sendDetailsToMVCDelegate, NSFetchedResultsControllerDelegate, NSCoding {

    @IBOutlet weak var tableView: UITableView!
    var namesListArray:[String] = []
    var imagesListArray:[UIImage] = []
    var delegateName: sendNameDelegate?
    var delegateImage: sendImageDelegate?

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        if let namesList = aDecoder.decodeObjectForKey("namesListArray") as? [String] {
            namesListArray = namesList
        } else {
            namesListArray = [String]()
        }
        if let imagesList = aDecoder.decodeObjectForKey("imagesListArray") as? [UIImage] {
            imagesListArray = imagesList
        } else {
            imagesListArray = [UIImage]()
        }
    }

    override func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(self.namesListArray, forKey: "namesListArray")
        aCoder.encodeObject(self.imagesListArray, forKey: "imagesListArray")
    }

    override func viewDidLoad() {
        let dir = getUserDir()
        let archiveName = "\(dir)/iRecipeList-namesListArray"
        if let loaded: AnyObject = NSKeyedUnarchiver.unarchiveObjectWithFile(archiveName) {
            self.namesListArray = (loaded as? [String])!
        }
        let archiveImage = "\(dir)/iRecipeList-imagesListArray"
        if let loaded: AnyObject = NSKeyedUnarchiver.unarchiveObjectWithFile(archiveImage) {
            self.imagesListArray = (loaded as? [UIImage])!
        }
    }

    override func viewDidAppear(animated: Bool) {
    }

    func sendDetailsToMVC (name: String, image: UIImage) {
        namesListArray.append(name)
        imagesListArray.append(image)
        let dir = getUserDir()
        let archiveName = "\(dir)/iRecipeList-namesListArray"
        NSKeyedArchiver.archiveRootObject(namesListArray, toFile: archiveName)
        let archiveImage = "\(dir)/iRecipeList-imagesListArray"
        NSKeyedArchiver.archiveRootObject(imagesListArray, toFile: archiveImage)
        tableView.reloadData()
    }

    func getUserDir() -> String {
        let userDir = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
        return userDir[0] as! String
    }


    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return namesListArray.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let row = indexPath.row
        let name = namesListArray[row]
        println("\(row)/")
        let image = imagesListArray[row]
        var cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: nil)
        cell.textLabel?.text = name
        cell.imageView!.image = image

        return cell }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        var row = indexPath.row

        var name = namesListArray[row]
        delegateName?.sendName(name)
        var image = imagesListArray[row]
        delegateImage?.sendImage(image)

        performSegueWithIdentifier("goToInfoVC", sender: self)
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "newReciep" {
            var vc = segue.destinationViewController as! DetailsViewController
            vc.delegateDetails = self
        }
    } }

并且:

import UIKit
import CoreData

class InformationViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, sendNameDelegate, sendImageDelegate {


    @IBOutlet var recipeNameLabel: UILabel!
    @IBOutlet var recipeImageView: UIImageView!
    @IBOutlet var RecipeHowToDo: UILabel!
    @IBOutlet var recipeIngredientsTableView: UITableView!
    var ingredientsListArray = [String]()

    func sendName(name: String) {
        recipeNameLabel.text = name
    }

    func sendImage(image: UIImage) {
        recipeImageView.image = image
    }


    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return ingredientsListArray.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let row = indexPath.row
        let ingredient = ingredientsListArray[row]
        var cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: nil)
        cell.textLabel!.text = ingredient
        return cell
    }

    override func viewDidLoad() {

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }
}

我似乎无法将此信息正确发送到最后一个视图控制器。有人有什么想法吗?

PS:抱歉图片太多,只是觉得有用。

您当前正在调用 sendName 和 sendImage 委托,然后再为它们赋值。这就是为什么他们没有访问您的 InformationViewController 的原因。你应该这样做:

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "newReciep" {
            var vc = segue.destinationViewController as! DetailsViewController
            vc.delegateDetails = self
        }else if(segue.identifier == "goToInfoVC"){
            var vc = segue.destinationViewController as! InformationViewController

            let row = self.tableView.indexPathForSelectedRow()?.row
            var name = namesListArray[row]
            var image = imagesListArray[row]
            vc.name = name
            vc.image = image

        } 
    }

然后在 infoViewController ViewDidLoad 方法中,您可以将 name 和 image 的值设置为您的视图。