在Swift中从Eureka Forms获取值 [英] Get values from Eureka Forms in Swift
问题描述
我是Swift中的新编程人员,正在尝试使用Eureka库创建表单.
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.
这是我的代码:
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
的正确行类型,我同时使用两种方法
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.
}
}
这篇关于在Swift中从Eureka Forms获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!