ios - 在多个 TextField 中填充数据(数字)之前,如何禁用 UIButton?

标签 ios swift iphone

我的应用程序中有 3 个 UITextFields,我需要用户先填写,然后他/她才能看到将计算结果的 UIButton。我尝试了很多不同的方法来解决这个问题,但它们似乎不起作用,而且我不太理解其中的一些方法。我相信有一种简单的方法可以做到这一点。 P.S.:我不使用 Storyboard

我已经尝试过包含UITextFieldDelegate,然后写了一个函数,也用了其他方式等等...

import UIKit
let Label: UILabel = {
    let f = UILabel()
    f.text = "Label"
    return f
}()
let textFiled1: UITextField = {
    let fw = UITextField()
    fw.keyboardType = UIKeyboardType.decimalPad
    return fw
}()
let textField2: UITextField = {
    let fh = UITextField()
    fh.keyboardType = UIKeyboardType.decimalPad
    return fh
}()
let textField3: UITextField = {
    let fa = UITextField()
    fa.keyboardType = UIKeyboardType.numberPad
    return fa
}()
class ViewController: UIViewController {
    let calculateButton: UIButton = {
        let c = UIButton()
        c.setTitle("Calculate", for: .normal)
        return c
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
        setupLabel()
        setupTextFieldComponents()
        setupCalculateButton()
    }
    fileprivate func setupLabel() {
        view.addSubview(fLabel)
    }
    fileprivate func setupTextFieldComponents() {
        setupTextField1()
        setupTextField2()
        setupTexrField3()
    }
    fileprivate func setupTextField1() {
        view.addSubview(textField1)
    }
    fileprivate func setupTextField2() {
        view.addSubview(textField2)
    }
    fileprivate func setupTextField3() {
        view.addSubview(textField3)
    }
    fileprivate func setupCalculateButton() {
        view.addSubview(calculateButton)
    }
    @objc func calculateButtonPressed(sender: UIButton){
        let textfield1 = Float(textField1.text!)
        let textfield2 = Float(textField2.text!)
        let textfield3 = Float(textField3.text!)
        femaleMetaLabel.text = String(111 + (1.1 * textfield1!) + (1.1 * textfield2!) - (1.1 * textfield3!))
    }
}

用户应该在所有三个文本字段中输入数字数据以计算结果。有没有办法在应用程序不崩溃的情况下弹出错误消息?

最佳答案

当我对启用按钮的文本输入进行验证时,我通常会这样做:


@IBOutlet weak var usernameField: UITextField!
@IBOutlet weak var passwordField: UITextField!
@IBOutlet weak var submitButton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()

    submitButton.isEnabled = false
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    NotificationCenter.default.addObserver(self,
                                           selector: #selector(validateInput(_:)),
                                           name: UITextField.textDidChangeNotification,
                                           object: usernameField)
    NotificationCenter.default.addObserver(self,
                                           selector: #selector(validateInput(_:)),
                                           name: UITextField.textDidChangeNotification,
                                           object: passwordField)
}

override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)

    NotificationCenter.default.removeObserver(self)
}

@objc func validateInput(_ sender: Any?) {

    submitButton.isEnabled = false

    if let username = usernameField.text,
        let password = passwordField.text {

        // your validation rules will be more complex than this :D
        if username.count > 0 && password.count > 0 {
            submitButton.isEnabled = true
        }
    }
}


如果您需要做的是在输入不正确时弹出警报,只需将调用替换为如下所示的方法,您可以在其中验证用户的输入。

func showAlert() {

    let alert = UIAlertController(title: "Nope",
                                  message: "Your inputs were incorrect.",
                                  preferredStyle: .alert)

    alert.addAction( UIAlertAction(title: "Try again",
                                   style: .default,
                                   handler: nil) )

    self.present(alert, animated: true, completion: nil)
}

关于ios - 在多个 TextField 中填充数据(数字)之前,如何禁用 UIButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55325153/

相关文章:

ios - Swift3 中奇怪的类名与 Core Data 冲突

iphone - 图片加载到 UIWebView 时无法放大缩小

ios - 使用 segues 使用来自 Parse 的数据填充 tableView

ios - 从场景加载节点后应用程序卡住

ios - 为 View Controller 设置方向景观

iphone - iOS 6 模拟器中的 ABAddresBook 权限

ios - SceneKit 计算可见边界?

ios - CollectionViewCell 不持久

ios - 为什么DispatchGroup会干扰主队列?

ios - UINavigation PushViewController 不显示下一个 View ,而是运行代码