我在一个学校项目的应用程序上工作。应用程序应管理不同的客户(使用客户名称和电子邮件地址)和任务(包括任务名称和每小时价格)。 然后应用程序应该根据此数据创建帐单。
我的问题是如何从两个选择器视图中获取客户和任务(存储在Core Data中)的数据?
链接到完整的项目: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()
}
}
我希望有人可以帮助我。 我已经说过了谢谢你的帮助。
答案 0 :(得分:0)
您可以获得以下代码数据:
这是您的控制器,请简单地复制粘贴并使其正常工作。
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()
}
}