How to run a button action in motionEnded?

53 views Asked by At

In a very basic shaking dice app in Swift 4, I have two functions. The first runs the random dice function. The second however needs a sender argument as it's really an IBAction so it needs to know which button is being pressed.

How do I add a fake button hit to motionEnded during the shake?

    // MARK: -- Outlets and Actions
    @IBOutlet weak var diceImageView1: UIImageView!
    @IBOutlet weak var diceImageView2: UIImageView!


    @IBAction func rollButtonPressed(_ sender: UIButton) {

        updateDiceImages()
        updateRollButton(sender)

    }

...

    override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
        updateDiceImages()

        // how to send sender from motionEnded?
        rollButtonPressed( magic sender needed )

        // or another function that also needs the UIButton sender
        updateRollButton( same magic sender needed )
    }
}
1

There are 1 answers

0
Tamás Sengel On BEST ANSWER

IBActions don't have to have a sender argument, you can remove it.

@IBAction func rollButtonPressed()

Note that after modifying the function, you should rewire the action inside Interface Builder to prevent crashing.