选择器视图中来自核心数据的数据

Posted

技术标签:

【中文标题】选择器视图中来自核心数据的数据【英文标题】:data from core data in picker view 【发布时间】:2017-06-02 09:52:03 【问题描述】:

我正在为一个学校项目开发一个应用程序。该应用程序应管理不同的客户(使用客户名称和电子邮件地址)和任务(使用任务名称和每小时价格)。 那么应用程序应该根据这些数据创建一个账单。

我的问题是如何在两个选择器视图中从客户和任务(存储在核心数据中)获取数据?

完整项目的链接:https://drive.google.com/open?id=0Bwwqhrg9CBVfTU50R2dBaF9ZcUk

(抱歉使用德语变量和函数名:/)

这是任务的资源

//
//  ArrayAufgabenResource.swift
//  TheServiceAppV2
//
//  Created by David Zerweck on 09.04.17.
//  Copyright © 2017 David Zerweck. All rights reserved.
//

////////////////////////////////////////////////////
    //German = English

    //Kunde = Customer
    //Aufgabe = Task
////////////////////////////////////////////////////
import Foundation
import CoreData

struct ArrayAufgabeResource: AufgabeResource 

var persistentContainer: NSPersistentContainer = 

    let container = NSPersistentContainer(name: "TheServiceAppV2")
    container.loadPersistentStores 
        (storeDescription, error) in

        if let error = error 
            let nserror = error as NSError
            fatalError("Fehler: \(nserror.localizedDescription)")
        
    
    return container
()

func saveContext() 
    let context = persistentContainer.viewContext
    if context.hasChanges 
        do 
            try context.save()
         catch 
            let nserror = error as NSError
            fatalError("Fehler: \(nserror.localizedDescription)")
        
    


func insertAufgabe(withName: String, andPreis: String) -> Aufgabe 
    let newAufgabe = NSEntityDescription.insertNewObject(forEntityName: "Aufgabe", into: persistentContainer.viewContext) as! Aufgabe

    newAufgabe.nameDerAufgabe = withName
    newAufgabe.preisProStunde = andPreis

    saveContext()
    return newAufgabe


func remove(aufgabe: Aufgabe) 
    persistentContainer.viewContext.delete(aufgabe)
    saveContext()


func getList() -> [Aufgabe] 
    var aufgaben = [Aufgabe]()

    let request: NSFetchRequest<Aufgabe> = Aufgabe.fetchRequest()
    do 
        aufgaben = try persistentContainer.viewContext.fetch(request)
     catch 
        print(error.localizedDescription)
    

    return aufgaben
   

这是客户资源

//
//  ArrayKundenResource.swift
//  TheServiceAppV2
//
//  Created by David Zerweck on 09.04.17.
//  Copyright © 2017 David Zerweck. All rights reserved.
//

import Foundation
import CoreData

struct ArrayKundeResource: KundeResource 

var persistentContainer: NSPersistentContainer = 

    let container = NSPersistentContainer(name: "TheServiceAppV2")
    container.loadPersistentStores 
        (storeDescription, error) in

        if let error = error 
            let nserror = error as NSError
            fatalError("Fehler: \(nserror.localizedDescription)")
        
    

    return container
()

func saveContext() 
    let context = persistentContainer.viewContext
    if context.hasChanges 
        do 
            try context.save()
         catch 
            let nserror = error as NSError
            fatalError("Fehler: \(nserror.localizedDescription)")
        
    


func insertKunde(withName: String, andEmail: String) -> Kunde 

    let newKunde = NSEntityDescription.insertNewObject(forEntityName: "Kunde", into: persistentContainer.viewContext) as! Kunde

    newKunde.name = withName
    newKunde.email = andEmail

    saveContext()

    return newKunde


func remove(kunde: Kunde) 
    persistentContainer.viewContext.delete(kunde)
    saveContext()



func getList() -> [Kunde] 
    var kunden = [Kunde]()

    let request: NSFetchRequest<Kunde> = Kunde.fetchRequest()
    do 
        kunden = try persistentContainer.viewContext.fetch(request)
     catch 
        print(error.localizedDescription)
    

    return kunden
 
  

在下面的 ViewController 中,应该有两个选择器视图。第一个选择器视图应该显示客户。第二个选择器视图应该显示任务。

//
//  RechnungViewController.swift
//  TheServiceAppV2
//
//  Created by David Zerweck on 17.05.17.
//  Copyright © 2017 David Zerweck. All rights reserved.
//

////////////////////////////////////////////////////
//German = English

//Kunde = Customer
//Aufgabe = Task
//Rechnung = Bill
////////////////////////////////////////////////////

import UIKit

class RechnungViewController: UIViewController 


@IBOutlet weak var pickerViewKunde: UIPickerView!

@IBOutlet weak var pickerViewAufgabe: UIPickerView!

@IBOutlet weak var anzahlStundenField: UITextField! //number of hours


@IBAction func rechnungBtn(_ sender: Any) 



override func viewDidLoad() 
    super.viewDidLoad()



override func didReceiveMemoryWarning() 
    super.didReceiveMemoryWarning()
 

我希望有人可以帮助我。 我已经在说谢谢你的帮助。

【问题讨论】:

【参考方案1】:

你可以得到如下代码的数据:

这是您的控制器,只需复制粘贴即可使用。

import UIKit

class RechnungViewController: UIViewController , UIPickerViewDelegate, UIPickerViewDataSource 


@IBOutlet weak var pickerViewKunde: UIPickerView!

@IBOutlet weak var pickerViewAufgabe: UIPickerView!

@IBOutlet weak var anzahlStundenField: UITextField!


var allKunden = [Kunde]()

@IBAction func rechnungBtn(_ sender: Any) 



override func viewDidLoad() 
    super.viewDidLoad()

    //Temp Add data
    var kun = Kunde()
    let arr : ArrayKundeResource = ArrayKundeResource()
    kun = arr.insertKunde(withName: "JIGAR", andEmail: "jigar@mail.com")
    print(kun.email)
    print(kun.name)
 //This is for temporary adding data inside coredata so you can remove this.

    //get all customer
    self.getAllCustomer()


func getAllCustomer() 

    let arr : ArrayKundeResource = ArrayKundeResource()
    self.allKunden = arr.getList()

    print(self.allKunden.count)


//pickerview datasource methods
func numberOfComponents(in pickerView: UIPickerView) -> Int 
    return 1


func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int 
    return self.allKunden.count


func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? 
    let kundObj = self.allKunden[row] as Kunde
    return kundObj.name


func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) 
    let kundObj = self.allKunden[row] as Kunde
    print(kundObj.name)


override func didReceiveMemoryWarning() 
    super.didReceiveMemoryWarning()



【讨论】:

以上是关于选择器视图中来自核心数据的数据的主要内容,如果未能解决你的问题,请参考以下文章

如何根据之前的选择器视图选择更新PickerView 数据

来自先前选择器的选择器值 - CoreData/SwiftUI

TabBar 项目 - 表格视图中的核心数据

选择器视图默认行未使用 swift 2 更改

当核心数据中找不到相关实体时,无法识别的选择器发送到实例

onAppear 不允许保存/更新选择器值的更改?