Search code examples
uibuttonswift3ibaction

Swift 3: How can I show/hide the @IBAction button inside the ViewDidLoad() func?


I am trying to show/hide a button only if the information is available. For example, I get the information from the database, and if the field returns blank the a button should be hidden else show the button.

@IBAction func emailBtn(_ sender: AnyObject) {
   //blank
}


viewdidload()
if emailURL.characters.count >= 5
    {
        emailBtn.isHidden = false //Giving error Please see the screenshot
    }
    else
    {
        emailBtn.isHidden = true // Giving error Please see the screenshot
    }


}

Screenshot


Solution

  • You're trying to hide an @IBAction function. What you're intending to do is hide the button. What you need to do is create an outlet in your code that references that button.

    In the Storyboard, Control+Drag the button to your class. Create an outlet. This will create something like the following:

    @IBOutlet weak var myButton: UIButton!
    

    You want to reference that button in viewDidLoad()

    myButton.isHidden = true