# Enable button when 2 textifleds are filled

**URL:** <https://forums.kodeco.com/t/enable-button-when-2-textifleds-are-filled/34697>\
**Category:** Swift\
**Created:** [September 21, 2017, 1:32am UTC](https://forums.kodeco.com/t/enable-button-when-2-textifleds-are-filled/34697 "2017-09-21T01:32:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![timswift](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/timswift/32/110937_2.png) [@timswift](https://forums.kodeco.com/u/timswift)\
**Post date:** [September 21, 2017, 1:32am UTC](https://forums.kodeco.com/t/enable-button-when-2-textifleds-are-filled/34697/1 "2017-09-21T01:32:13Z")

</div>

I am trying to not enable a button until both textfields are not nil. Thats it. I tried experimenting with viewdidappear but its not working. As soon as both textfields are not nil the button remains isenable = false.

```
                 import UIKit

class ViewController: UIViewController {
@IBOutlet var saveButton: UIButton!

@IBOutlet var textB: UITextField!
@IBOutlet var textA: UITextField!
override func viewDidLoad() {
    super.viewDidLoad()
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(true)

    safeGuard()
}
func safeGuard() {
    if textA.text == "" || textB.text == ""{
        saveButton.isEnabled = false
    } else {
        saveButton.isEnabled = true
    }}}

```

 ![16](https://raywenderlich-com-v3-forums.s3.amazonaws.com/original/3X/6/2/627a32160d1a7ea8bfd5060c5c7189d58d5e188b.png)

---

<div class="post-metadata">

**Author:** ![nikita\_gaydukov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/nikita_gaydukov/32/77683_2.png) [@nikita\_gaydukov](https://forums.kodeco.com/u/nikita_gaydukov)\
**Post date:** [September 21, 2017, 8:25am UTC](https://forums.kodeco.com/t/enable-button-when-2-textifleds-are-filled/34697/2 "2017-09-21T08:25:04Z")

</div>

Hi @timswift

If this is all your code, you call the safeGuard function only once - when a view did appear on the screen.  
What you need to do is to call this function every time one of your textfields changed it’s content.  
Take a look at this answer on SO - [ios - UITextField text change event - Stack Overflow](https://stackoverflow.com/questions/7010547/uitextfield-text-change-event)

Hope this helps

Nikita

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [March 6, 2018, 5:32pm UTC](https://forums.kodeco.com/t/enable-button-when-2-textifleds-are-filled/34697/3 "2018-03-06T17:32:19Z")

</div>

Closed on March 6, 2018, 5:32pm UTC

This topic was automatically closed after 166 days. New replies are no longer allowed.
