在视图swift

时间:2016-04-16 19:31:53

标签: swift core-data nsfetchrequest

我目前正在开发一个库存应用程序,该应用程序将保存两个实体的信息。第一个实体拥有两个属性“站点名称”和“站点地址”。第二个实体持有“itemName”“itemQuantity”和“item picture”。我能够将数据保存到核心数据。我现在遇到了获取数据的问题。有没有人知道如何在视图控制器之间传递数据。

我正在上传网站视图控制器,详细视图控制器和我的新项目视图控制器。

class SiteTableViewController: UIViewController {


    var managedContext : NSManagedObjectContext = CoreDataHelper.sharedInstance.myContext
    var sites : [Site]? = [Site]()

    //variable to hold selected site
    var selectedSite : Site?

    let myCellIdentifier = "cell"



    @IBOutlet weak var tableView: UITableView!


    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self

        // Do any additional setup after loading the view.
        sites = [Site]()
        reloadData()

        automaticallyAdjustsScrollViewInsets = false


        //let barButtonItem = UIBarButtonItem(barButtonSystemItem: .Compose, target: self, action: "addSite:")
        //self.navigationItem.leftBarButtonItems = [barButtonItem]
    }




    //load new data into table 
    func reloadData() {

        let fetchRequest = NSFetchRequest(entityName: "Site")
        let sortDescriptor = NSSortDescriptor(key: "siteName", ascending: true)

        fetchRequest.sortDescriptors = [sortDescriptor]

        do {
            sites = try self.managedContext.executeFetchRequest(fetchRequest) as! [Site]
            tableView.reloadData()

        } catch {

            print("Error fetching objects")

        }
        tableView.reloadData()


    }



    override func viewWillAppear(animated: Bool) {

       reloadData()

    }



}

extension SiteTableViewController: UITableViewDataSource, UITableViewDelegate {

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

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

        let site = sites![indexPath.row]

        cell.textLabel?.text = site.valueForKey("siteName") as? String
        cell.detailTextLabel?.text = site.valueForKey("siteAddress") as? String

        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 logItemToDelete = sites![indexPath.row]
        sites?.removeAtIndex(indexPath.row)
        managedContext.deleteObject(logItemToDelete)

            do {

                try managedContext.save()

            } catch {

                print("Could not save or delete object")
            }

             tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        }

    }





    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return sites!.count 

    }

//    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//        
//        selectedSite = sites![indexPath.row]
//        
//    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        if segue.identifier == "detailSegue" {

            var nextVC = segue.destinationViewController as! DetailTableViewController
            var selectedItem : Site = sites![self.tableView.indexPathForSelectedRow!.row] as Site

            fetchSite = selectedItem.valueForKey("siteName") as? NSManagedObject
            nextVC.valueToPass = selectedItem.siteName!

        }

    }

}

这是我的详细视图控制器

import UIKit
import CoreData

public var fetchSite : NSManagedObject?

class DetailTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var holdSiteData: NSMutableArray = NSMutableArray()
    var managedContext : NSManagedObjectContext = CoreDataHelper.sharedInstance.myContext


    @IBOutlet weak var tableView: UITableView!


    var sites : [Site]!


    //string to hold fetchResults controll



    //open string to hold siteName
    var valueToPass = ""

    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationItem.title = "\(valueToPass)" 

        // Do any additional setup after loading the view.

        tableView.delegate = self
        tableView.dataSource = self

        automaticallyAdjustsScrollViewInsets = false
        tableView.estimatedRowHeight = 75.0
        tableView.rowHeight = UITableViewAutomaticDimension

        retrieveItemRelationship()

    }

    override func viewWillAppear(animated: Bool) {

        retrieveItemRelationship()
    }



    func retrieveItemRelationship() {

       // fetchSite.setValue(NSSet(keyCommands), forKey: "test")
        let fetchRequest = NSFetchRequest(entityName: "SiteItem") 
        let sortDescriptors = NSSortDescriptor(key: "itemName", ascending: true)
        fetchRequest.sortDescriptors = [sortDescriptors]

        do {

            let result = try fetchSite?.managedObjectContext?.executeRequest(fetchRequest)


        } catch let error as NSError {

            print(error)
        }


    }





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

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

    //    let cellInfo = fetchedResultsController.objectAtIndexPath(indexPath) as! SiteInfo



        cell.cellName?.text = "Test"




        return cell
    }



    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return holdSiteData.count


    }


}

这是我保存数据的最后一个视图。我想我需要在这里传递数据,然后关联这两个实体,以便我可以正确保存它。例如,站点名称“Staples Center”。我需要将其拉到最终视图控制器,然后将信息保存到此对象。

import UIKit
import CoreData

class ItemNewViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    //var managedObjectContext : NSManagedObjectContext!

    var managedContext : NSManagedObjectContext = CoreDataHelper.sharedInstance.myContext


    //categories array for picker view
    var categories = ["Building Materials", "Electrical", "Cleaning Products",
        "Tools & Hardware", "Plumbing", "Paint", "Appliances", "Other"]

    //textFields

    @IBOutlet weak var itemNameTextField: UITextField!
    @IBOutlet weak var quantityTextField: UITextField!



    @IBOutlet weak var imageView: UIImageView!


    //Label properties
    @IBOutlet weak var newItemLabel: UILabel!
    @IBOutlet weak var quantityLabel: UILabel!
    @IBOutlet weak var categoryLabel: UILabel!


    //pickerLabel
    @IBOutlet weak var pickerListIcon: UIPickerView!



    //clicker property label and textlabel for number
    @IBOutlet weak var increaseNumberClicker: UIStepper!
    @IBOutlet weak var numberFieldClicker: UITextField!




    override func viewDidLoad() {
        super.viewDidLoad()

        //label property customize view code
        newItemLabel.layer.masksToBounds = true
        newItemLabel.layer.borderWidth = 2.0
        newItemLabel.layer.borderColor = UIColor.whiteColor().CGColor
        newItemLabel.layer.cornerRadius = 6

        //label property customize view code
        quantityLabel.layer.masksToBounds = true
        quantityLabel.layer.borderWidth = 2.0
        quantityLabel.layer.borderColor = UIColor.whiteColor().CGColor
        quantityLabel.layer.cornerRadius = 6

        //lable property customize view code
        categoryLabel.layer.masksToBounds = true
        categoryLabel.layer.borderWidth = 3.0
        categoryLabel.layer.borderColor = UIColor.whiteColor().CGColor
        categoryLabel.layer.cornerRadius = 6

        //label picker customize view
        pickerListIcon.layer.masksToBounds = true
        pickerListIcon.layer.borderWidth = 3.0
        pickerListIcon.layer.borderColor = UIColor.whiteColor().CGColor
        pickerListIcon.layer.cornerRadius = 6
        pickerListIcon.layer.backgroundColor = UIColor.blackColor().CGColor



        self.pickerListIcon.dataSource = self
        self.pickerListIcon.delegate = self


        numberFieldClicker.enabled = false


        //UIStepper value
        increaseNumberClicker.wraps = true
        increaseNumberClicker.autorepeat = true
        increaseNumberClicker.layer.masksToBounds = true
        increaseNumberClicker.layer.borderWidth = 3.0
        increaseNumberClicker.layer.borderColor = UIColor.whiteColor().CGColor
        increaseNumberClicker.layer.cornerRadius = 6
        increaseNumberClicker.layer.backgroundColor = UIColor.blackColor().CGColor



        //below code declares a variable that inherits from UITapGesture
        //this allows the image view to be an action
        //*Make sure you enable user interaction in storyBoard very IMPORTANT!
        let imageTapRecognizer = UITapGestureRecognizer(target: self, action: "enableImage")

        self.imageView.addGestureRecognizer(imageTapRecognizer)
    }


    // below function allows the camera view to be accessible
    func enableImage(){

        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) {

            let cameraView = UIImagePickerController()
            cameraView.sourceType = UIImagePickerControllerSourceType.Camera
            cameraView.delegate = self

            self.presentViewController(cameraView, animated: true, completion: nil)

        }

    }

    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {

        self.imageView.image = image

        picker.dismissViewControllerAnimated(true, completion: nil)
    }



    //function used to increase UIStepper. increment by integers of one
    @IBAction func clickerIncreaser(sender: UIStepper) {



        numberFieldClicker.text = Int(sender.value).description


    }


    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {

        return 1
    }


    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

        return categories.count
    }



    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

        return categories[row]
    }


    func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {

        //passing the categorories array into the new constant myCategoriesArray
        let myCategoriesArray = categories[row]

        let myTextColor = NSAttributedString(string: myCategoriesArray, attributes: [NSForegroundColorAttributeName: UIColor.whiteColor()])

        return myTextColor
    }


    //save data to core data


    @IBAction func saveButton(sender: AnyObject) {



       if (itemNameTextField.text?.isEmpty)! {

        let displayAlertController = UIAlertController(title: "Form Incomplete", message: "Please provide item with a name", preferredStyle: .Alert)


        let okButton : UIAlertAction = UIAlertAction(title: "OK", style: .Default, handler: nil)

        displayAlertController.addAction(okButton)

        self.presentViewController(displayAlertController, animated: true, completion: nil)

       } else {


        //declare site object and object
        let siteInfoEntity = NSEntityDescription.entityForName("SiteItem", inManagedObjectContext: self.managedContext)

       //declare item and item object

        let siteObject = SiteItem(entity: siteInfoEntity!, insertIntoManagedObjectContext: self.managedContext)

        //convert String to Integer 
        let quantityNumber: Int? = Int(quantityTextField.text!)

        siteObject.setValue(itemNameTextField.text?.capitalizedString, forKey: "itemName")
        siteObject.setValue(quantityNumber, forKey: "itemQnty")

        fetchSite?.setValue(NSSet(object: siteObject), forKey: "reverseItems")

        self.managedContext.saveOrLogError()


        self.navigationController?.popViewControllerAnimated(true)

     }

    }


}

再一次,如果有人可以指出我正确的方向来保存我与核心数据的关系,那么检索这些关系会很棒。核心数据还是新手,所以需要我能得到的所有帮助!

谢谢

1 个答案:

答案 0 :(得分:0)

您的详细信息控制器可以拥有属性Site。在prepareForSegue中,您将选定的Site对象传递给详细控制器。

网站列表和项目列表都应该由NSFetchedResultsController支持,而不是内存数组。