在核心数据中保存uipickerview颜色

时间:2015-09-25 13:00:28

标签: ios swift core-data uipickerview

我知道自己想要什么,但我只是努力以正确的方式去做。我试图实现一个小应用程序,让用户通过提供类别名称,描述然后从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()
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您可以通过这种方式获取颜色名称:

doubleencode

例如,你可以在你的函数中使用它:

let index = pickerView.selectedRowInComponent(0)
let color = categoryColor[index]

答案 1 :(得分:0)

didSelectRow中获取categoryColor

的值
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
       let colorString = categoryColor[row] as! String

        // Save the colorString into Core data

    }