Search code examples
iosswiftcore-datauibuttonuipickerview

How to store UIButton value in core data?


How to store UIButton value in core data.I have two button named as BloodGroup and city. when user click on any button then pickerview appear at bottom and there is cancel and done button.So user select value from UIPickerview and click on Done button then UIPickerview disappear and that value displayed in UIButton.

Answer needed for two questions. I want to store this value in core data. 1) how we can store this value?

second option is if we can store this value in uitextfield then it is easy.

2) can any one tell me how to pass data from selected field in button to UITextField ?

import UIKit

class Bloodbank: UIViewController {


    @IBOutlet weak var Bloodgroup: UIButton!

    @IBOutlet weak var City: UIButton!


    @IBOutlet weak var pickerViewObj: UIPickerView!


    @IBOutlet weak var subViewObj: UIView!



    let fruitsArrayValues = ["A+","A-","B+","B-","AB+","AB-","O+","O-"]
    let iPhoneModelArrayValues = ["Ahmedabad","Gandhinagar","Surat","Rajkot","Vadodara"]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        subViewObj.hidden = true
    }

    //delegate methods.
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        //dynamic delegate methods using pickerviewObj.tag
        if pickerViewObj.tag == 0 {
            return fruitsArrayValues.count
        }
        else {
            return iPhoneModelArrayValues.count
        }
    }

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
        if pickerViewObj.tag == 0 {
            return "\(fruitsArrayValues[row])" //its displyed to titleForRow.
        }
        else {
            return "\(iPhoneModelArrayValues[row])"
        }
    }

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        //valuesLbl.text = pickerDataValues[row]
    }


    @IBAction func selectItemsBtn(sender: AnyObject) {

        //first they set the tag in pickerviewObj.
        subViewObj.hidden = false
        switch sender.tag {
        case 0:
            pickerViewObj.tag = 0
        case 1:
            pickerViewObj.tag = 1
        default:
            break;
        }
        pickerViewObj.reloadAllComponents()
    }



    @IBAction func cancelBtn(sender: AnyObject) {
    subViewObj.hidden = true
    }


    @IBAction func DoneBtn(sender: AnyObject) {

        if pickerViewObj.tag == 0 {
            let selectedIndex = pickerViewObj.selectedRowInComponent(0)
            Bloodgroup.setTitle(fruitsArrayValues[selectedIndex], forState: UIControlState.Normal)
        }
        else if pickerViewObj.tag == 1 {
            let selectedIndex = pickerViewObj.selectedRowInComponent(0)
            City.setTitle(iPhoneModelArrayValues[selectedIndex], forState: UIControlState.Normal)
        }
        subViewObj.hidden = true

    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

Solution

  • As i can see the selected value is just a String. You can store strings in core data.

    For the second question: you can get the value of the button's title with the current title method. See: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIButton_Class/#//apple_ref/occ/instp/UIButton/currentTitle

    Btw have you considered using NSUserDefaults to store this value? It might be easier for you.