0
votes

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

enter image description here

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


}
1
Do you use synchronize() method to update your defaults?Alex Kosyakov
I would recommend using core data. NSUserDefaults is just for storing little pieces of data. Some people don't like core data because it is a bit confusing. You can check out alternatives online to make it easier!Dan Levy
Try converting the UIImage to NSData before attempting to store it in NSUserDefaults.kye
Here is a video with the solution guys. Thx youtube.com/watch?v=1kCKlv1npw0JoseMartinFit

1 Answers

2
votes

NSUserDefaults is not intended for storing large objects like images. I would suggest storing your images to the documents directory, and then saving paths to the images into defaults.