[英]Save uipickerview color in core data
我知道我想要的,但我只是在努力以正確的方式進行操作。 我正在嘗試實現一個小型應用程序,該應用程序允許用戶通過提供類別名稱,描述然后從uipickerview中選擇顏色來添加類別。 我已經成功將名稱和描述保存在核心數據中。 我只是在努力如何獲取選定的顏色字符串並將其保存在核心數據中。 任何幫助將不勝感激。 到目前為止,這是我編寫的代碼:
import UIKit
import CoreData
class NewCategory: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
let categoryColor = ["Red","Yellow","Black","White", "Green", "Blue"]
// MARK: - Properties
let context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
// MARK: Outlets
@IBOutlet weak var name: UITextField!
@IBOutlet weak var descriptionField: UITextView!
@IBOutlet weak var pickerView: UIPickerView!
@IBOutlet weak var colorLabel: UILabel!
// MARK: Actions
@IBAction func saveBtn(sender: AnyObject) {
if name.text == "" || name.text.isEmpty || name.text.isEmpty{
var alert = UIAlertController(title: "WARNING !!!", message: "Couldn't add category. Fill all fields.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
} else {
createCategory() }
}
// MARK: View settings
override func viewDidLoad() {
super.viewDidLoad()
self.pickerView.dataSource = self
self.pickerView.delegate = self
}
// Custom functions
func createCategory() {
let entity = NSEntityDescription.entityForName("Category", inManagedObjectContext: context!)
let category = Category(entity: entity!, insertIntoManagedObjectContext: context)
category.name = name.text
category.descript = descriptionField.text
category.color = colorLabel
println(category.name)
context?.save(nil)
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return categoryColor.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return categoryColor[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if (row == 0) {
colorLabel.backgroundColor = UIColor.redColor()
}
else if(row == 1) {
colorLabel.backgroundColor = UIColor.yellowColor()
}
else if(row == 2) {
colorLabel.backgroundColor = UIColor.blackColor()
}
else if(row == 3) {
colorLabel.backgroundColor = UIColor.whiteColor()
}
else if(row == 4) {
colorLabel.backgroundColor = UIColor.greenColor()
}
else {
colorLabel.backgroundColor = UIColor.blueColor()
}
}
}
您可以通過以下方式獲得顏色名稱:
let index = pickerView.selectedRowInComponent(0)
let color = categoryColor[index]
例如,您可以像下面這樣在函數中使用它:
func createCategory() {
let entity = NSEntityDescription.entityForName("Category", inManagedObjectContext: context!)
let category = Category(entity: entity!, insertIntoManagedObjectContext: context)
category.name = name.text
category.descript = descriptionField.text
let index = pickerView.selectedRowInComponent(0)
let color = categoryColor[index]
category.color = color // (assuming color is a String)
println(category.name)
context?.save(nil)
}
在didSelectRow
從categoryColor
獲取值
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let colorString = categoryColor[row] as! String
// Save the colorString into Core data
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.