在视图之间传递属性或托管对象ID - 核心数据

时间:2014-10-13 16:51:01

标签: ios core-data swift nsmanagedobject

enter code here使用Swift和Xcode 6。

  

查看One.swift = TableView。

     

查看Two.swift =查看w / TextFields(以及其他UI元素),根据TableView中选择的项目进行更改

我编写了将TableView中选择的托管对象ID传递给View Two(作为委托属性)。 但这是最好的方法吗?

    //main view class
    var mainAry = [String]() // for TableView
    var objectsAry = [NSManagedObject]()
    var itemObChosen: NSManagedObject? = nil  //protocol property

    //loads the table view and objectsAry
    func loadMyItems() {
           objectsAry.removeAll() 
           let appDel: AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
           let contextOb: NSManagedObjectContext = appDel.managedObjectContext!
           let cdFetch = NSFetchRequest(entityName: "Items")
           var getData = contextOb.executeFetchRequest(cdFetch, error: nil)!

           if(getData.count > 0) {
                for eachItem in getData {
                    var itemItem = eachItem.valueForKey("itemname") as String!
                    if (itemItem != nil) {
                    objectsAry.append(eachItem as NSManagedObject)
                    }
                }  
            }
        }

    //within didSelectRowAtIndexPath 
    itemClkdVar = indexPath.row
    itemObChosen = objectsAry[itemClkdVar] //passed to the other view as chosen object

    //fixed to eliminate mainAry
maincell.textLabel?.text = self.objectsAry[indexPath.row].valueForKey("itemname") as? String

0 个答案:

没有答案