Reputation: 188
Here's the problem: I'm trying to pass along some data from one ViewController to another InfoViewController. It looks like this:
Running, the app, it looks like this:
Beside this two view controllers, I have some other ones in this app and their Segues are working perfectly, but, when I tap the cell, it does go to the InfoViewController, but the information does not get updated.
Here's my code:
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
}
} }
And:
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()
}
}
I can't seem to properly send this information to the last view controller. Does anyone have any ideas?
PS: Sorry about the great amount of images, just thought it would be helpful.
Upvotes: 1
Views: 107
Reputation: 1531
You are currently calling sendName and sendImage delegates before assigning them any value . Thats why they are not getting to your InformationViewController. You should do something like that:
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
}
}
Then in the infoViewController ViewDidLoad method you can set the value of name and image to your views.
Upvotes: 1