iForm 框架

iForm 是一个 Swift 框架,旨在通过其内置的方法集简化表单构建过程。

安装 iForm

您可以轻松地通过 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))

				}
}
  1. 我们首先初始化我们的 iFormSignInForm
  2. 使用对象的 initSignInForm() 方法创建表单。
  3. 将您希望提交按钮执行的操作作为 actionToPerform 参数传递。
    1. 在该回调中,我们通过 iForm 方法 getTextFieldsValues() 获取生成的文本字段的值。
    2. 然后我们将它们存储在变量中。
    3. 最后,我们简单地将它们传递给我们的 DetailViewController。
  4. 使用 display() 方法将表单添加到定义的视图中,并应用定义的约束。

注意:getTextFieldsValues() 接收一个 UITextField 表作为参数,该表可以通过 getAllTextFields() 方法返回。

定义

iForm

约束

iFormButtonItem

iFormTextFieldItem

UIButtonStyle

UITextFieldStyle