Bright Future Bright Future - 4 months ago 26
iOS Question

How to disable button touch at the start in Swift

I have this button:

@IBAction func touch(sender: AnyObject) {
println("Hello, world!")
}


and another:

@IBAction func toggle(sender: AnyObject) {
//code to enable touch of button "touch"


I want to disable user touch of "touch" at the start of the app, and enable touch after tapping "toggle", how to realize it by code and in storyboard?

Answer

You need @IBOutlets to access the buttons at runtime. In InterfaceBuilder ctrl-drag the button to the top of your custom class file, choose Outlet and give it a descriptive name. It will look like this:

class MyViewController: UIViewController {
    @IBOutlet weak var touch: UIButton!
    @IBOutlet weak var toggle: UIButton!

    // Disable in `viewDidLoad`:
    override func viewDidLoad() {
        super.viewDidLoad()

        self.touch.enabled = false
    }

    @IBAction func toggle(sender: AnyObject) {
        //code to enable touch of button "touch"
        self.touch.enabled = true
    }
}
Comments