We have an App where we are trying to program some travels, save them in NSUserDefaults and retrieve them in a tableView. In order to do that we have two viewControllers.
Here is the idea
As you can see, in the first viewController (programViewController.swift) we have three UITextFields :
destinationTextField, dateTextField, companyTextField.
And a UIImageView with a button to choose a photo with UIImagePickerViewController.
To finish we have a button named "programar viaje" or program travel. When we click this the app save all our fields in NSUserDefaults
The second ViewController (travelViewController.swift) has a table view to display the travels.
Ok, I'm able to save all the textfields, but I'm not able to save the photo correctly in NSUserDefaults and show it in the table of travelViewController.swift .
Where I'm wrong guys?
import UIKit
let defaults = NSUserDefaults.standardUserDefaults()
**var destinationArray = String
var dateArray = String
var companyArray = String
var pictureArray = UIImage**
class programViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UITextViewDelegate {
@IBOutlet weak var destinyTextField: UITextField!
@IBOutlet weak var dateTextField: UITextField!
@IBOutlet weak var companyTextField: UITextField!
@IBOutlet weak var companyPicture: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func choosePhoto(sender: AnyObject) {
let picker = UIImagePickerController()
picker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
picker.allowsEditing = true
picker.delegate = self
self.presentViewController(picker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
self.companyPicture.image = image
self.dismissViewControllerAnimated(true, completion: nil)
}
@IBAction func programButton(sender: AnyObject) {
destinationArray.append(destinyTextField.text!)
dateArray.append(dateTextField.text!)
companyArray.append(companyTextField.text!)
pictureArray.append(companyPicture.image!)
defaults.setObject(destinationArray, forKey: "destinyUD")
defaults.setObject(dateArray, forKey: "dateUD")
defaults.setObject(companyArray, forKey: "companyUD")
defaults.setObject(pictureArray, forKey: "pictureUD")
print("Destinos \(destinationArray), Fechas \(dateArray) , Compañía \(companyArray) , Imágenes \(pictureArray)")
self.performSegueWithIdentifier("programSegue", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "programSegue" {
if let vc : travelViewController = segue.destinationViewController as? travelViewController {
vc.passedDestination = destinationArray
vc.passedDate = dateArray
vc.passedCompany = companyArray
vc.passedPicture = pictureArray
}
}
}
class travelViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var passedDestination = []
var passedDate = []
var passedCompany = []
var passedPicture = []
override func viewDidLoad() {
super.viewDidLoad()
print("Soy viewdidload")
if NSUserDefaults.standardUserDefaults().objectForKey("destinyUD") != nil {
destinationArray = NSUserDefaults.standardUserDefaults().objectForKey("destinyUD") as! [String]
}
if NSUserDefaults.standardUserDefaults().objectForKey("dateUD") != nil {
dateArray = NSUserDefaults.standardUserDefaults().objectForKey("dateUD") as! [String]
}
if NSUserDefaults.standardUserDefaults().objectForKey("companyUD") != nil {
companyArray = NSUserDefaults.standardUserDefaults().objectForKey("companyUD") as! [String]
}
if NSUserDefaults.standardUserDefaults().objectForKey("pictureUD") != nil {
pictureArray = NSUserDefaults.standardUserDefaults().objectForKey("companyUD") as! [UIImage]
}
}
override func viewWillAppear(animated: Bool) {
//print("Soy viewDidAppear")
}
override func viewDidAppear(animated: Bool) {
tableView.reloadData()
print("Estoy recargando la tabla")
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//return fecha.count
return destinationArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell : customCell2 = (tableView.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath) as? customCell2)!
cell.cellDestination.text = destinationArray[indexPath.row]
cell.cellDate.text = dateArray[indexPath.row]
cell.cellCompany.text = companyArray[indexPath.row]
cell.cellImage2.image = pictureArray[indexPath.row] as? UIImage
return cell
}
UIImage
toNSData
before attempting to store it inNSUserDefaults
. – kye