我必须ViewController
和两个班MasterViewController.swift
和LoginViewController.swift
。在头等课程中,我有tableView
,您可以在其中添加记录,并在课程ViewController
中打开新的DetailViewController.swift
,其中textView
我可以添加文字,当我去的时候回到tableView
它必须保存我的文字,但是当我这样做时它不会保存。这是来自MasterViewController
的鳕鱼:
import UIKit
import CoreData
class MasterViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, NSFetchedResultsControllerDelegate {
@IBOutlet var tableView: UITableView!
var isAuthenticated = false
var managedObjectContext: NSManagedObjectContext? = nil
var _fetchedResultsController: NSFetchedResultsController? = nil
var didReturnFromBackground = false
override func awakeFromNib() {
super.awakeFromNib()
}
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.leftBarButtonItem = self.editButtonItem()
view.alpha = 0
let addButton = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "insertNewObject:")
self.navigationItem.rightBarButtonItem = addButton
NSNotificationCenter.defaultCenter().addObserver(self, selector: "appWillResignActive:", name: UIApplicationWillResignActiveNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "appDidBecomeActive:", name: UIApplicationDidBecomeActiveNotification, object: nil)
}
@IBAction func unwindSegue(segue: UIStoryboardSegue) {
isAuthenticated = true
view.alpha = 1.0
}
func appWillResignActive(notification : NSNotification) {
view.alpha = 0
isAuthenticated = false
didReturnFromBackground = true
}
func appDidBecomeActive(notification : NSNotification) {
if didReturnFromBackground {
self.showLoginView()
}
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(false)
self.showLoginView()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func showLoginView() {
if !isAuthenticated {
self.performSegueWithIdentifier("loginView", sender: self)
}
}
func insertNewObject(sender: AnyObject) {
let context = self.fetchedResultsController.managedObjectContext
let entity = self.fetchedResultsController.fetchRequest.entity!
let newManagedObject = NSEntityDescription.insertNewObjectForEntityForName(entity.name!, inManagedObjectContext: context) as! NSManagedObject
newManagedObject.setValue(NSDate(), forKey: "date")
newManagedObject.setValue("New Note", forKey: "noteText")
var error: NSError? = nil
if !context.save(&error) {
abort()
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {
if let indexPath = self.tableView.indexPathForSelectedRow() {
let object = self.fetchedResultsController.objectAtIndexPath(indexPath)as! NSManagedObject
(segue.destinationViewController as! DetailViewController).detailItem = object
}
}
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return self.fetchedResultsController.sections?.count ?? 0
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let sectionInfo = self.fetchedResultsController.sections![section] as! NSFetchedResultsSectionInfo
return sectionInfo.numberOfObjects
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
self.configureCell(cell, atIndexPath: indexPath)
return cell
}
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
let context = self.fetchedResultsController.managedObjectContext
context.deleteObject(self.fetchedResultsController.objectAtIndexPath(indexPath) as! NSManagedObject)
var error: NSError? = nil
if !context.save(&error) {
abort()
}
}
}
func configureCell(cell: UITableViewCell, atIndexPath indexPath: NSIndexPath) {
let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as! NSManagedObject
cell.textLabel?.text = object.valueForKey("noteText")!.description
}
@IBAction func logoutAction(sender: AnyObject) {
isAuthenticated = false
self.performSegueWithIdentifier("loginView", sender: self)
}
var fetchedResultsController: NSFetchedResultsController {
if _fetchedResultsController != nil {
return _fetchedResultsController!
}
let fetchRequest = NSFetchRequest()
let entity = NSEntityDescription.entityForName("Note", inManagedObjectContext: self.managedObjectContext!)
fetchRequest.entity = entity
fetchRequest.fetchBatchSize = 20
let sortDescriptor = NSSortDescriptor(key: "date", ascending: false)
let sortDescriptors = [sortDescriptor]
fetchRequest.sortDescriptors = [sortDescriptor]
let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext!, sectionNameKeyPath: nil, cacheName: "Master")
aFetchedResultsController.delegate = self
_fetchedResultsController = aFetchedResultsController
var error: NSError? = nil
if !_fetchedResultsController!.performFetch(&error) {
abort()
}
return _fetchedResultsController!
}
func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
switch type {
case .Insert:
self.tableView.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
case .Delete:
self.tableView.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
default:
return
}
}
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
switch type {
case .Insert:
tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
case .Delete:
tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
case .Update:
self.configureCell(tableView.cellForRowAtIndexPath(indexPath!)!, atIndexPath: indexPath!)
case .Move:
tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
default:
return
}
}
}
我的DetailViewControoler.swift:
import UIKit
import CoreData
class DetailViewController: UIViewController, UITextViewDelegate {
let ManagedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate)
@IBOutlet weak var detailTextView: UITextView!
var note: Note? = nil
var detailItem: AnyObject? {
didSet {
self.configureView()
}
}
func configureView() {
if let detail: Note = self.detailItem as? Note {
if let detailTextView = self.detailTextView {
detailTextView.text = detail.noteText
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.configureView()
}
func textViewDidEndEditing( textView: UITextView) {
if let detail: Note = self.detailItem as? Note {
if let detailTextView = self.detailTextView {
detail.noteText = detailTextView.text
}
}
ManagedObjectContext.managedObjectContext!.save(nil)
}
}