我想實現在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
}
}
我希望以上提供的信息綽綽有餘,您對正確方向的支持將非常感謝。
謝謝
您可以使用行傳遞填充到數組中的數據。 –