I am new to ios development but have 36+ year of programming experience. I am developing a new app that uses an @IBAction to connect multiple buttons. When the user presses a button, I want to than disable the button until the app has completed processing the asssociated code/ How do I set the button state to disabled?
Question regard disabling button in Swift
@IBAction methods usually have a sender argument:
@IBAction func myAction(sender: AnyObject){...}
//or
@IBAction func myAction(sender: UIButton) {...}The sender argument defaults to a type of "AnyObject", but it's possible to set the sender's type to UIButton when you create the action. Even if you don't, you can either downcast it from AnyObject to UIButton in the action, or edit your source code to change "AnyObject" to "UIButton". The later works if you know the action will only be called by UIButton instances.
Once you have the sender as a UIButton, you can set the sender's enabled state to false:
sender.enabled = falseWith this approach, you're sure to set the enabled state on the button that initiated the action.
Alternatively, if you also make an IBOutlet for the button, you can refer to the button by the IBOutlet (a property) as opposed to using the sender argument, which lets you change the state from other places in your code, such as after the processing has completed.
@IBOutlet weak var myButton: UIButton!
// then elsewhere in your code, within a method implementation:
myButton.enabled = trueYou can review the header file for UIControl.h to view the enabled/selected/highlighted properties that UIButtons inherit from UIControl. I suggest using the File > Open Quickly menu item (or associated shortcut key combination) then type "UIButton.h" to view the many properties UIButton has. Then move to its superclass, UIControl.
Hope this helps.