我正在尝试在IOS中实现功能,用户点击UIPickerController即didSelectRow,并将该数据发送到另一个视图控制器
我正在使用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
以上是我的获取数据方法,它从Core Data
获取数据然后我通过我的加载方法
将其加载到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)
大家好我解决了我的问题
根据我的理解&#39; Core Data&#39;不确定技术术语是什么。在创建新信息示例实体名称表属性名称
时name = cat
该名称将具有其ID,此ID称为objectId。您可以使用objectId通过字符串匹配和其他技术获取数据。但是,如果您想使用数组
使用旧时尚方法索引值将信息传递给多个表单那么这应该可以解决你的问题
首先从Core数据获取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
当然,您需要将数组存储在gobal的某个位置,以便可以从任何视图控制器访问它
现在您需要将数据加载到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
确保您在视图中运行这些控件按以下顺序执行加载方法
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) {
}
请记住,大部分代码都是从我的orignal代码编辑的,有些变量可能会显示为未声明等...
如果你觉得这很有用,请给予肯定。如果我错了,也要纠正我。我们在这里学习。
谢谢