2017-04-14 79 views
0

我想实现在IOS的功能,使用户点击UIPickerController即didSelectRow和数据发送到另一个视图控制器CoreData如何将数据发送到另一个视图控制器

我使用的IOS 10,CoreData这里是一瞥我如何做到这一点。

// fetch data from the Core data model 
    func fetchData(){ 
    // fetch the entity 
    //1st stage:what is going to fetchgo and fetch ProductDetails 
    let fetchRequest : NSFetchRequest<ProductDetails> 
     = ProductDetails.fetchRequest() 
    // perform the above action : 
    do { 

      self.data = try context.fetch(fetchRequest) 

     for d in data { 

      coursetitle = d.courseName! 
      print("course title is: \(coursetitle)") 

     } 


      self.PickerCourse.reloadAllComponents() 

    } 
    catch { 
     // handle any errors 

     let error = error as NSError 
     print("A Error has occurred while fetching data: \(error) !") 

    }// end of catch statement 


}// end of fetch data method 

以上是获取从核心数据的数据

我那么这个通过我的load方法加载到UIPicker

//加载数据到选择器

func loadProductData(){ 

    if let item = productsToEdit { 
     // can invoke relational ie toProductDetails 
     if let store = item.toStore{ 

      var index = 0 
      repeat{ 
       // populate in UIPicker 
       let sIndex = data[index] 
       if sIndex.courseName == store.name 
       { 
        PickerCourse.selectRow(index, 
        inComponent: 0, animated: false) 
        break // out of the loop 
       } 
       index += 1 // increment index by 1 

      }while (index < data.count) 
     } 

    }// end of item check 

}// end of load product data 
我取数据的方法

这里是问题

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, 

    inComponent component: Int) { 


    if component == 0{ 

     // what ever the row is selected assign the value to variable. 
     CourseSelectedIndex = row 
     productData = [data[row]] 
     print("user has selected row at: \(productData)") 
     //even if I include a segue here I get a error state Array can 
     // not be assign to NSObject 


    } 


} 

我希望以上提供的信息绰绰有余,您对正确方向的支持将非常感谢。

谢谢

+0

您可以使用行传递填充到数组中的数据。 –

回答

0

大家好我解决我的问题

不知道的技术术语是从我的理解“核心数据”创建一个新的信息,例如实体名称表时,属性名称

名= cat

该名称的id将被称为objectId。您可以使用objectId通过字符串匹配和其他技术来获取数据。但是如果你想使用数组

然后用旧的方式方法的指数值将信息传递给多种形式这应该解决您的问题

你从核心数据第一fetchdata并将其加载到阵列

var ArrayName: [String] = [] 

func fetchData(){ 

    print("you are outside the do loop fetch 
     method for debug purpose only ") 

    do { 

     data = try context.fetch(ProductDetails.fetchRequest()) 
     print("you are inside the do ...") 

     for each in data { 

      print("Course Name : \(each.courseName!) 
      \n Price : \(each.price)\n \n Description: 
       \(each.desription) \n") 


      ArrayName.append(each.Name!)// append to a array 
      print("You are in the Fetch method ...") 

     }// end of for each 


    } 
    catch { 
     // handle any errors 


     print("What a nob you are indeed !") 



    }// end of catch statement 


}// end of fetch data method 

当然,您需要将阵列存储在某个地方,以便您可以从任何视图控制器访问它

现在您需要将数据加载到UIPicker控制器中

//将数据加载到选取器中 //为核心数据创建一个NS对象 var productsToEdit:ProductDetails? FUNC loadProductData(){

if let item = productsToEdit { 
     // can invoke relational ie toProductDetails 
     if let store = item.toStore{ 

      var index = 0 
      repeat{ 

       let sIndex = data[index] 
       if sIndex.Name == store.name 
       { 
        PickerCourse.selectRow(index, 
        inComponent: 0, animated: false) 
        break // out of the loop 
       } 
       index += 1 // increment index by 1 

      }while (index < data.count) 
     } 

    }// end of item check 

}// end of load product data 

确保在运行这些鉴于控制研究做load方法按以下顺序

loadData() fetchData()

也在你UIPicker控制器确保你有以下

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


    return ArrayName[row] 

} 

func pickerView(_ pickerView: UIPickerView, 
numberOfRowsInComponent component: Int) -> Int { 
    // return number of rows within UIPickerView 
    return data.count 
} 

func numberOfComponents(in pickerView: UIPickerView) -> Int { 
    // return the number of ' Colums to display 

    return 1 
} 



func pickerView(_ pickerView: UIPickerView, 
didSelectRow row: Int, inComponent component: Int) { 

    if component == 0{ 

     // what ever the row 
     is selected assign the value to variable. 
     CourseSelectedIndex = row 

     print("user has selected row at: \(productData)") 

    } 


} 

func pickerView(_ pickerView: UIPickerView, 
didSelectRowAtIndexPath indexPath: NSIndexPath) { 



} 

请记住,大部分代码已经从我的信号代码中编辑过一些变量可能会显示为不声明等...

如果您发现这个有用的,请给予功劳。如果我错了,也要纠正我。我们在这里学习。

谢谢

相关问题