iForm 是一个 Swift 框架,旨在通过其内置的方法集简化表单构建过程。
您可以轻松地通过 XCode 项目的Packages Dependency(软件包依赖项)部分添加该软件包。
使用 iForm 构建登录表单相对容易。
import UIKit
import iForm
class SignInViewController: UIViewController, UITextFieldDelegate {
private var form: iForm = iForm()
override func viewDidLoad() {
super.viewDidLoad()
var formular: SignInForm = SignInForm()
//Initialize sign-in form
formular = form.initSignInForm(
//Submit button callback
actionToPerform : UIAction() { _ in
//Retrieving the values in our text fields
let textFieldsValues = self.form.getTextFieldsValues(from: self.form.getAllTextFields(from: formular))
//Storing the data
let email = textFieldsValues["Email"]
let password = textFieldsValues["Password"]
let detailsVC = DetailsViewController()
detailsVC.email = email
detailsVC.password = password
//Passing the data to the next View Controller
self.navigationController?.pushViewController(detailsVC, animated: true)
})
//Add the form to the defined view
form.display(formular, on: self.view, withConstraints: Constraints(horizontal: 0, vertical: -250, width: 0, height: 50))
}
}
iForm 和 SignInForm。initSignInForm() 方法创建表单。actionToPerform 参数传递。iForm 方法 getTextFieldsValues() 获取生成的文本字段的值。display() 方法将表单添加到定义的视图中,并应用定义的约束。注意:getTextFieldsValues() 接收一个 UITextField 表作为参数,该表可以通过 getAllTextFields() 方法返回。