0

In first view controller i saved data in core data with uitextfields, i wanted to tableview to read that data in second view controller, i got no errors but i got no display of data in tableview cells. In first VC i made a property of NSManaged object, and in second VC i retrieve that object, put it in tableview but no data is displayed. This is crucial to my understanding about how tableview can read data from core data, thank you a lot.

First VC

import UIKit
import CoreData

class ViewController: UIViewController {

@IBOutlet weak var nameField: UITextField!

@IBOutlet weak var passField: UITextField!

var item = [NSManagedObject]()

override func viewDidLoad() {
    super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

@IBAction func saveInfo(sender: UIButton) {

    let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let context: NSManagedObjectContext = appDel.managedObjectContext

    //add new user
    let newUser = NSEntityDescription.insertNewObjectForEntityForName("Users", inManagedObjectContext: context)
    newUser.setValue(nameField.text, forKey: "username")
    newUser.setValue(passField.text, forKey: "passwords")

    // add the info to the entity
    do{
        try context.save()
    }catch{
        print("Error, data not saved!")
    }

    // retrieve data

    do{

        let request = NSFetchRequest(entityName: "Users")
        let results = try context.executeFetchRequest(request)

        if results.count > 0 {

            for item in results as! [NSManagedObject]{

                let name = item.valueForKey("username")
                let password = item.valueForKey("passwords")

                print(name!, password!)
        }
        }

        }catch{

        print("Erorr, data not read!")
    }

    nameField.text = ""
    passField.text = ""

    }// end of button function


}

Second VC aka Tableview

import UIKit
import CoreData

class TableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var tableView: UITableView!

var bridge: ViewController = ViewController()

var array = [NSManagedObject]()

override func viewDidLoad() {
    super.viewDidLoad()

    self.array = self.bridge.item

    self.tableView.delegate = self
    self.tableView.dataSource = self
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

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

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

    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)

    let person = array[indexPath.row]
    cell.textLabel!.text = person.valueForKey("username") as? String
    return cell
}

}
ciccioska
  • 1,291
  • 16
  • 22
Igy
  • 129
  • 1
  • 16
  • Did you forget to call self.tableView.reloadData() in viewDidLoad? – Shripada Sep 30 '15 at 10:50
  • This might help http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – Vinay Jain Sep 30 '15 at 10:56
  • i passed nsmanaged object with this self.array = self.bridge.item – Igy Sep 30 '15 at 10:59
  • 2
    The problem is that `var bridge: ViewController = ViewController()` creates a *new instance* of ViewController, unrelated to the existing instance. Rather than trying to pull the data into the second VC from the first, you should push it from the first VC to the second, when you create/segue to the second VC. – pbasdf Sep 30 '15 at 13:15
  • i can't thank you enough man, i think that's it. Will try. – Igy Sep 30 '15 at 14:59
  • i tried with preparer segue but i also get no data displayed – Igy Oct 01 '15 at 10:28
  • I'm a little late to this question, but you may wanna try 'unwind segues' that helped me – CoolMAn Sep 10 '16 at 04:44

0 Answers0