所以我正在制作这个xCode
项目,用Swift
语言编写iPhone应用程序。我试图在核心数据的对象中存储两个字符串和二进制数据图像,我可以在此viewcontroller
中检索。我用来转换到此视图的表格视图显示正确的项目关联,但是当我加载此视图时,属性不会加载。为什么对象没有上传并显示到viewcontroller
?
import UIKit
import CoreData
class IndividualMainViewController: UIViewController, NSFetchedResultsControllerDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate
{
@IBOutlet weak var backCancelButton: UIBarButtonItem!
@IBOutlet weak var saveEditButton: UIBarButtonItem!
@IBOutlet weak var deviceImageButton: UIButton!
@IBOutlet weak var cameraImageButton: UIButton!
var item: Item? = nil
let moc = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
let frc: NSFetchedResultsController = NSFetchedResultsController()
@IBOutlet weak var imageHolder: UIImageView!
@IBOutlet weak var horseName: UITextField!
@IBOutlet weak var officialName: UITextField!
override func viewDidLoad()
{
super.viewDidLoad()
if(item == nil)
{
print("item returned nil")
print(item)
}
else
{
print("Object did load")
self.horseName.text = item?.name
self.officialName.text = item?.offName
self.imageHolder.image = UIImage(data: (item?.image)!)
saveEditButton.title = "Edit"
backCancelButton.title = "To Barn"
self.deviceImageButton.hidden = true
self.cameraImageButton.hidden = true
}
let tap: UIGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissEditor")
view.addGestureRecognizer(tap)
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func dismissEditor()
{
view.endEditing(true)
}
@IBAction func addImageFromDevice(sender: AnyObject)
{
let pickerController = UIImagePickerController()
pickerController.delegate = self
pickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
pickerController.allowsEditing = true
self.presentViewController(pickerController, animated: true, completion: nil)
}
@IBAction func addImageFromCamera(sender: AnyObject)
{
let pickerController = UIImagePickerController()
pickerController.delegate = self
pickerController.sourceType = UIImagePickerControllerSourceType.Camera
pickerController.allowsEditing = true
self.presentViewController(pickerController, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?)
{
self.dismissViewControllerAnimated(true, completion: nil)
self.imageHolder.image = image
}
@IBAction func saveEditTapped(sender: AnyObject)
{
if(saveEditButton.title == "Save")
{
if item != nil
{
editItem()
print("Item edit saved")
saveEditButton.title = "Edit"
backCancelButton.title = "To Barn"
}
else
{
createNewItem()
print("New item created")
performSegueWithIdentifier("back", sender: nil)
}
}
else
{
saveEditButton.title = "Save"
backCancelButton.title = "Cancel"
print("Not saved")
}
}
func createNewItem()
{
let entityDescription = NSEntityDescription.entityForName("Item", inManagedObjectContext: moc)
let item = Item(entity: entityDescription!, insertIntoManagedObjectContext: moc)
item.name = horseName.text
item.offName = officialName.text
item.image = UIImagePNGRepresentation(imageHolder.image!)
do
{
try self.moc.save()
}
catch
{
print("Failed ot create new object")
return
}
}
func editItem()
{
item?.name = horseName.text
item?.offName = officialName.text
item!.image = UIImagePNGRepresentation(imageHolder.image!)
do
{
try self.moc.save()
}
catch
{
print("Failed to save item")
return
}
}
}
答案 0 :(得分:0)
仅使用通用初始化程序初始化NSFetchedResultsController
是不够的。
您必须告诉控制器要执行的操作,使用哪个托管对象上下文,设置委托并执行初始提取。
Swift中的常用方法是将控制器声明为惰性计算变量。
lazy var fetchedResultsController: NSFetchedResultsController = {
let fetchRequest = NSFetchRequest()
let entity = NSEntityDescription.entityForName(self.entity, inManagedObjectContext: self.managedObjectContext)
fetchRequest.entity = entity
fetchRequest.predicate = self.fetchPredicate
// Set the batch size to a suitable number.
fetchRequest.fetchBatchSize = 20
// Edit the sort key as appropriate.
fetchRequest.sortDescriptors = [self.sortDescriptor]
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext!, sectionNameKeyPath: nil, cacheName: nil)
aFetchedResultsController.delegate = self
do {
try aFetchedResultsController.performFetch()
} catch let error as NSError {
print(error)
}
return aFetchedResultsController
}()
将实例变量(以self.
开头)表示的所有参数替换为值