簡體   English   中英

在Swift中從Eureka Forms獲取值

[英]Get values from Eureka Forms in Swift

我是Swift中的新編程人員,正在嘗試使用Eureka庫創建表單。

表單已經可以使用,但是我無法從表單中獲取數據。

我試圖將數據一個一個地存儲到全局變量中,以便在按下按鈕時將其打印出來。

問題是代碼總是在中斷,我不知道如何糾正它。

這是我的代碼:

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!

    }

謝謝你的時間

一旦DateRow或TextRow更改,您就需要使用.onChange來更新您的值,或者您可以使用form.rowBy(tag: "tagName")直接訪問該值,並強制轉換為可以訪問.value的正確行類型。在此示例代碼中,使用您的基本代碼,我同時使用兩種方法

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.
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM