简体   繁体   中英

How to enable/disable navigation bar buttons [XCode] (swift)

I was wondering how I would be able to go about changing the status of buttons. I want to make it if one of two text fields has text in them then the button will become useable. I have currently turned off the button from the storyboard. The code I have to check if there is text inside of the text fields is as follows:

Disclaimer: The code to check if the text field has any text in it works perfectly fine.

    @IBAction func textFeildEditingChanged(_ textField: UITextField) {

        if FirstName.hasText == true {

            self.navigationItem.rightBarButtonItem?.isEnabled = true

            print("First name isn't empty")
        }
    }

The current code that I have in there to set the button to enabled and disable doesn't work however the code to test if the text field has content does work. I just need to figure out how to disable and enable the button of a navigation item.

code that doesn't work is below:

self.navigationItem.rightBarButtonItem?.isEnabled = true

Any help would be greatly appreciated.

Edit: I am using a navigation controller, don't know if that's important or not.

If you are using the storyboard, just connect the outlet then disable it.

@IBOutlet var navigationItemButton: UIBarButtonItem!

then

navigationItemButton.isEnabled = FirstName.hasText

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM