Get values from Eureka Forms in Swift

2019-07-12 02:09发布

问题:

I'm new programming in Swift and I'm trying to create a form using Eureka Library.

The form is already working but I can't get the data out of the form.

I'm trying to get the data one by one store it into a global variable in order to be printed when the button is pressed.

The thing is the code is always breaking and I don't know how to correct it.

This is my code:

import UIKit
import Eureka

class ViewController: FormViewController
{
    //Creating Global Variables
    var name: String = ""
    var data: Date? = nil

    @IBAction func testebutton(_ sender: Any)
    {
        print(name)
        print(data)
    }

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

        form +++ Section()
            <<< TextRow()
                {
                    row in
                row.title = "Name"
                row.tag = "name"
            }
            <<< DateRow()
                {
                $0.title = "Birthdate"
                $0.value = Date()
                $0.tag = "date"
                }

        //Gets value from form
        let row: TextRow? = form.rowBy(tag: "name")
        let nome = row?.value

        name = nome!

    }

Thanks for your time

回答1:

You need to use the .onChange to update your values once DateRow or TextRow changes, or you can access directly to the value with form.rowBy(tag: "tagName") and casting to the correct type of row you can access to .value in this example code using your base code I use both approaches

import UIKit
import Eureka

class GettingDataViewController: FormViewController {

    //Creating Global Variables
    var name: String = ""
    var data: Date? = nil

    @IBAction func testebutton(_ sender: Any)
    {
        print(name)
        print(data)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        form +++ Section()
            <<< TextRow()
                {
                    row in
                    row.title = "Name"
                    row.tag = "name"
                }.onChange({ (row) in
                    self.name = row.value != nil ? row.value! : "" //updating the value on change
                })
            <<< DateRow()
                {
                    $0.title = "Birthdate"
                    $0.value = Date()
                    $0.tag = "date"
                }.onChange({ (row) in
                    self.data = row.value  //updating the value on change
                })
            <<< ButtonRow(tag: "test").onCellSelection({ (cell, row) in
                print(self.name)
                print(self.data)

                //Direct access to value
                if let textRow = self.form.rowBy(tag: "name") as? TextRow
                {
                    print(textRow.value)
                }

                if let dateRow = self.form.rowBy(tag: "date") as? DateRow
                {
                    print(dateRow.value)
                }

            })

    }

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

}