禁用UIButton w / textFieldDidBeginEditing需要单击UITextField才能生效

时间:2014-09-23 00:43:25

标签: ios swift uibutton uitextfielddelegate

使用UITextFieldDelegate,我禁用了UIButton" btnSignup"根据3 UITextFields是否包含信息。目前,它适当地工作,减去UIButton被动(再次通过if语句)的事实,我必须点击UITextField。我输入的东西也是如此,我回去删除它。 UIButton保持有效,直到我点击UITextField为止(如果我点击提交而不点击其他地方,我就可以提交,这不应该是这种情况。)

class MainVC: UIViewController, UITextFieldDelegate {

    @IBOutlet var receiveName: UITextField!
    @IBOutlet var receiveEmail: UITextField!
    @IBOutlet var receivePhone: UITextField!
    @IBOutlet var btnSignup: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.btnSignup.enabled = false
        self.btnSignup.alpha = 0.3

        self.receiveName.delegate = self
        self.receiveEmail.delegate = self
        self.receivePhone.delegate = self

    func textFieldDidBeginEditing(textField: UITextField) {

        if countElements(receiveName.text) > 0 && countElements(receiveEmail.text) > 0 && countElements(receivePhone.text) > 0 {
            self.btnSignup.enabled = true
            self.btnSignup.alpha = 1
        } else {
            self.btnSignup.enabled = false
            self.btnSignup.alpha = 0.3
        }
    }
}

我正在寻找能够提供更实时效果的解决方案。如果正在编辑UITextField,该函数应该是侦听和响应。

4 个答案:

答案 0 :(得分:1)

使用接近代码的东西我可以这样做,如下所示。我添加了3级课程。记录文本字段输入内容的变量。我在文本字段中添加了标签。我更改了UITextFieldDelegate类func并测试了所有三个字段都有文本。第二个你在第三个字段中输入一个字段,按钮亮起!

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {
    var receiveNameBool = false
    var receiveEmailBool = false
    var receivePhoneBool = false

    @IBOutlet var receiveName: UITextField!
    @IBOutlet var receiveEmail: UITextField!
    @IBOutlet var receivePhone: UITextField!
    @IBOutlet var btnSignup: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.btnSignup.enabled = false
        self.btnSignup.alpha = 0.3

        self.receiveName.delegate = self
        self.receiveEmail.delegate = self
        self.receivePhone.delegate = self

        }

    func textField(textField: UITextField!, shouldChangeCharactersInRange range: NSRange, replacementString string: String!) -> Bool {

        if textField.tag == 1 {
            receiveNameBool = true
        } else if textField.tag == 2 {
            receiveEmailBool = true
        } else if textField.tag == 3 {
            receivePhoneBool = true
        }

        if receiveNameBool && receiveEmailBool && receivePhoneBool {
            self.btnSignup.enabled = true
            self.btnSignup.alpha = 1
        } else {
            self.btnSignup.enabled = false
            self.btnSignup.alpha = 0.3
        }
        return true
    }
}

好的,在第二个版本中,我使用了您使用的textFieldDidBeginEditing函数。在这种情况下,您在第三个字段内单击第二个按钮已启用。在这两个示例中,如果用户删除了他们输入的数据,您仍需要编写代码来处理案例。但上面的第一个例子可以为您提供所需的内容。

导入UIKit

class ViewController: UIViewController, UITextFieldDelegate {
    var receiveNameBool = false
    var receiveEmailBool = false
    var receivePhoneBool = false

    @IBOutlet var receiveName: UITextField!
    @IBOutlet var receiveEmail: UITextField!
    @IBOutlet var receivePhone: UITextField!
    @IBOutlet var btnSignup: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.btnSignup.enabled = false
        self.btnSignup.alpha = 0.3

        self.receiveName.delegate = self
        self.receiveEmail.delegate = self
        self.receivePhone.delegate = self

        }

    func textFieldDidBeginEditing(textField: UITextField) {

        if textField.tag == 1 {
            receiveNameBool = true
        } else if textField.tag == 2 {
            receiveEmailBool = true
        } else if textField.tag == 3 {
            receivePhoneBool = true
        }

        if receiveNameBool && receiveEmailBool && receivePhoneBool {
            self.btnSignup.enabled = true
            self.btnSignup.alpha = 1
        } else {
            self.btnSignup.enabled = false
            self.btnSignup.alpha = 0.3
        }
    }
}

答案 1 :(得分:0)

为此,您可以使用文本字段委托方法 stringByReplacingCharactersInRange

enum tags: Int{

case receiveNameTag = 1, receiveEmailTag, receivePhoneTag


}

导入UIKit

类ViewController:UIViewController,UITextFieldDelegate {

@IBOutlet var receiveName: UITextField!
@IBOutlet var receiveEmail: UITextField!
@IBOutlet var receivePhone: UITextField!
@IBOutlet var btnSignup: UIButton!

// here first assign the true because all the textfields are empty
var isReceiveNameTextFieldEmpty = true
var isReceiveEmailTextFieldEmpty = true
var isReceivePhoneTextFieldEmpty = true

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    self.btnSignup.enabled = false
    self.btnSignup.alpha = 0.3


    //here adding tags to the individual textfield and this is the best way to use tags
    receiveName.tag = tags.receiveNameTag.toRaw()
    receiveEmail.tag = tags.receiveEmailTag.toRaw()
    receivePhone.tag = tags.receivePhoneTag.toRaw()

    self.receiveName.delegate = self
    self.receiveEmail.delegate = self
    self.receivePhone.delegate = self

}

func textField(textField: UITextField!, shouldChangeCharactersInRange range: NSRange, replacementString string: String!) -> Bool {

    textField.text = (textField.text as NSString).stringByReplacingCharactersInRange(range,
        withString:string)

    //checking whether textfield is empty or not
    if countElements(textField.text) > 0 {

        if textField.tag == tags.receiveNameTag.toRaw() {
            isReceiveNameTextFieldEmpty = false
        }
        if textField.tag == tags.receiveEmailTag.toRaw() {
            isReceiveEmailTextFieldEmpty = false
        }
        if textField.tag == tags.receivePhoneTag.toRaw() {
            isReceivePhoneTextFieldEmpty = false
        }

    }
    else {

        if textField.tag == tags.receiveNameTag.toRaw() {
        isReceiveNameTextFieldEmpty = true
        }
        if textField.tag == tags.receiveEmailTag.toRaw() {
            isReceiveEmailTextFieldEmpty = true
        }
        if textField.tag == tags.receivePhoneTag.toRaw() {
            isReceivePhoneTextFieldEmpty = true
        }

    }

    //if atleast one textfield is empty disable button other wise enable
    if isReceiveNameTextFieldEmpty && isReceiveEmailTextFieldEmpty && isReceivePhoneTextFieldEmpty {
        self.btnSignup.enabled = false
        self.btnSignup.alpha = 0.3
    }
    else {
        self.btnSignup.enabled = true
        self.btnSignup.alpha = 1
    }


}

}

答案 2 :(得分:0)

我的一个应用程序遇到了类似的问题。它发生在我身上,因为代表是在后台线程上调用的,后者线程不会立即更新视图。您需要将所有ui更改同步到主线程。请参阅以下代码:

class MainVC: UIViewController, UITextFieldDelegate {

    @IBOutlet var receiveName: UITextField!
    @IBOutlet var receiveEmail: UITextField!
    @IBOutlet var receivePhone: UITextField!
    @IBOutlet var btnSignup: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.btnSignup.enabled = false
        self.btnSignup.alpha = 0.3

        self.receiveName.delegate = self
        self.receiveEmail.delegate = self
        self.receivePhone.delegate = self

    func textFieldDidBeginEditing(textField: UITextField) {
        dispatch_sync(dispatch_get_main_queue(), {
            if countElements(self.receiveName.text) > 0 && countElements(self.receiveEmail.text) > 0 && countElements(self.receivePhone.text) > 0 {
                self.btnSignup.enabled = true
                self.btnSignup.alpha = 1
            } else {
                self.btnSignup.enabled = false
                self.btnSignup.alpha = 0.3
            }
        })
    }
}

此外,如果您的countElements函数是您班级的本地函数,请在通话前添加self.

答案 3 :(得分:0)

可以使用textField(_:shouldChangeCharactersInRange:replacementString:)来解决 只需使用您的定义定义此方法,如下所示:

func textField(textField: UITextField!, shouldChangeCharactersInRange range: NSRange, replacementString string: String!) -> Bool {

        if countElements(receiveName.text) > 0 && countElements(receiveEmail.text) > 0 && countElements(receivePhone.text) > 0 {
            self.btnSignup.enabled = true
            self.btnSignup.alpha = 1
        } else {
            self.btnSignup.enabled = false
            self.btnSignup.alpha = 0.3
        }
        return true
  }

我认为countElements()正在计算字符串的长度。