我的核心数据项目中有2个VC。在VC1中,我有一个表视图,用于在我的coredata数据库中显示名称。在我的VC2中,我输入实体的详细信息。现在我想在用户选择特定行时更新我的数据库。当用户选择一行时,它将移动到VC2并在文本字段中显示名称,如果用户更新其中的任何内容,它将更新。我的代码如下。请建议我接下来要做什么。
输入详细信息 -
import UIKit
import CoreData
class EnterDetailViewController: UIViewController {
@IBOutlet weak var nametextfield: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func savedata(sender: AnyObject)
{
let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedcontext = appdelegate.managedObjectContext
let entity = NSEntityDescription.entityForName("Person", inManagedObjectContext: managedcontext)
let person = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: managedcontext)
person.setValue(self.nametextfield.text, forKey: "name")
do
{
try managedcontext.save()
print("SAVED")
}
catch let error as NSError
{
print("could not save \(error), \(error.userInfo)")
}
self.navigationController?.popToRootViewControllerAnimated(true)
}
}
VC1-在表格视图中显示
import UIKit
import CoreData
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
@IBOutlet weak var tableview: UITableView!
var people = [NSManagedObject]()
let manai = UIApplication.sharedApplication().delegate as! AppDelegate
//var person :NSMutableArray = []
override func viewDidLoad() {
super.viewDidLoad()
tableview.delegate = self
tableview.dataSource = self
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(animated: Bool)
{
print("CALLING FETCHDATA")
self.fetchData()
print("RELOADING DATA")
self.tableview.reloadData()
}
func fetchData()
{
let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appdelegate.managedObjectContext
let fetchRequest = NSFetchRequest(entityName: "Person")
do
{
people = try managedContext.executeFetchRequest(fetchRequest) as! [NSManagedObject]
print(people)
print("FETCHING DATA")
}
catch let error as NSError
{
print("could not fetch \(error), \(error.userInfo)")
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return people.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = self.tableview.dequeueReusableCellWithIdentifier("cellreuse", forIndexPath: indexPath)
let person = people[indexPath.row]
cell.textLabel?.text = person.valueForKey("name") as? String
return cell
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
{
let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appdelegate.managedObjectContext
if editingStyle == .Delete
{
managedContext.deleteObject(people[indexPath.row])
}
do
{
try managedContext.save()
print("SAVED")
}
catch let error as NSError
{
print("could not save \(error), \(error.userInfo)")
}
}
fetchData()
self.tableview .reloadData()
print("reload after delete")
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if segue.identifier == "segueupdate"
{
}
}
}
答案 0 :(得分:0)
NSManagedObject
的指定初始化程序是
initWithEntity:insertIntoManagedObjectContext:
。您无法调用通用初始值设定项NSManagedObject()
。
在您的情况下,您可以将managedobjectt
声明为(隐式解包)可选
var managedobjectt = NSManagedObject!
为了确保在VC2中访问变量不会太快,您还可以将viewDidLoad()
中的代码移至viewWillAppear()