xmartlabs / Eureka

Elegant iOS form builder in Swift
https://eurekacommunity.github.io
MIT License
11.77k stars 1.33k forks source link

How to get value and id_value for PushRow in Swift4 #1615

Closed EventfulG closed 6 years ago

EventfulG commented 6 years ago

I use xCode 9, swift 4 and "Eureka form library" for my project.

The situation

I have a list of cars with name and unique ID associated this way: 0 - ANY, 1 - VW, 7 - AUDI, 20 - MAZDA

var name_cars: [String] = ["ANY","VW","AUDI","MAZDA"]

var id_cars:[Int] = [0, 1, 7, 20]

I also have a form with "PushRow" and "ButtonRow". On click to the button I want to print the selected car name and ID. I was able to print the car's name but not the ID.

import UIKit
import Eureka

class myPage: FormViewController {

    var cars: [String] = ["ANY","VW","AUDI","MAZDA"]
    var id_cars:[Int] = [0, 1,7,20]

    var selected_car: String = "ANY" //default car
    var selected_car_id: Int = 0 //default id car

    override func viewDidLoad() {
        super.viewDidLoad()

        create_form()
    }

    func create_form(){

        form
        +++ Section("List")

        //list
        <<< PushRow<String>() {
            $0.title = "Cars"
            $0.options = cars
            $0.value = "ANY"

            $0.tag = "list_element"
            $0.selectorTitle = "Choose car"
            $0.onChange { [unowned self] row in
                self.selected_car = row.value!
                self.selected_car_id = ??? // **what should it be here in order to get the ID**
            }

        }

        //button
        <<< ButtonRow("Button1") {row in
            row.title = "Get Value on Console"
            row.onCellSelection{[unowned self] ButtonCellOf, row in
                print ("Car selected = ",self.selected_car, " and Id_Car_Selected = ",self.selected_car_id)
            }
        }
   }
}
EventfulG commented 6 years ago

After more digging I found the solution here https://stackoverflow.com/questions/51423153/how-to-get-value-and-id-value-for-pushrow-in-swift4

radvansky-tomas commented 6 years ago

Yes but this solution will not work for for self.form.values() as constructed Dictionary will contain full object, not just an ID