Turnipdabeets
Turnipdabeets

Reputation: 6005

Swift 4: access IBAction sender from another function?

I'm working on card game app and I set the dealThreeMore sender to disabled when the we run out of game.cards.

When I reset the game however I'm not sure how to access this sender button to reenable it. Is there some way I can access it without the user actually pressing this button and outside of the dealThreeMore func?

Github

@IBAction func dealThreeMore(_ sender: UIButton) {
        // have a match and cards available to deal
        if !matched.isEmpty && !game.cards.isEmpty {
            clearAndDeal()
        } else {
            // deal three more cards
            //TODO: make sure no set exists before allow dealing more?
            deal()
        }
        // disable if we run out of cards
        if game.cards.isEmpty {
            disable(button: sender)
            resetInfoLabel()
        }
    }

private func disable(button sender: UIButton){
    sender.isEnabled = false
    sender.backgroundColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 0.5160798373)
}

Upvotes: 0

Views: 353

Answers (1)

CZ54
CZ54

Reputation: 5588

You can create an @IBOutlet property to keep a reference to your button.

@IBoutlet weak var mMyButton : UIButton?

Then create the connection in your storyboard / xib file and you will be able to use mMyButton.isSelected = false

Upvotes: 2

Related Questions