传递数据和segues

时间:2015-06-06 22:24:25

标签: ios swift uiviewcontroller delegates segue

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

This one will pass data:

to this one here

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

This screen, when tapped on the cell, goes to:

This screen here.

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

这是我的代码:

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:对于大量的图片感到抱歉,只是觉得它会有所帮助。

1 个答案:

答案 0 :(得分:1)

您在为其分配任何值之前,当前正在调用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方法中,您可以为视图设置名称和图像的值。