gpt4 book ai didi

ios - CoreData 如何将数据发送到另一个 View Controller

转载 作者:行者123 更新时间:2023-11-30 12:32:01 25 4
gpt4 key购买 nike

我正在尝试在 IOS 中实现功能,其中用户单击 UIPickerController 即 didSelectRow 并将数据发送到另一个 View Controller

我使用的是 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


}


}

我希望以上提供的信息足够多,非常感谢您在正确方向上的支持。

谢谢

最佳答案

大家好,我解决了我的问题

在创建新信息示例实体名称表属性名称时,根据我对“核心数据”的理解,不确定技术术语是什么

名字=猫

名称将有其 ID,该 ID 称为 objectId。您可以使用 objectId 通过字符串匹配和其他技术来获取数据。但是,如果您想使用旧时尚方法使用数组索引值将信息传递到多个表单

那么这应该可以解决你的问题

首先从核心数据中获取数据并将其加载到数组中

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

当然,您需要将数组存储在全局某个地方,以便您可以从任何 View Controller 访问它

现在您需要将数据加载到 UIPicker Controller

//将数据加载到选择器中//为核心数据创建一个NS对象 var productsToEdit:产品详细信息? 函数 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

确保您在 View 控件中按以下顺序运行这些加载方法

加载数据()获取数据()

还在你的 UIPicker Controller 中确保你有以下内容

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) {



}

请记住,此代码的大部分内容都是从我的原始代码中编辑的,其中某些变量可能会显示为未声明等...

如果您发现这有用,请给予信任。如果我错了,同样纠正我。我们来这里是为了学习。

谢谢

关于ios - CoreData 如何将数据发送到另一个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43407938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com