2017-04-14 78 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) { 



} 

請記住,大部分代碼已經從我的信號代碼中編輯過一些變量可能會顯示爲不聲明等...

如果您發現這個有用的,請給予功勞。如果我錯了,也要糾正我。我們在這裏學習。

謝謝

相關問題