Search code examples
iosfunctionbuttonswift3ibaction

How to use function to call button instead of pressing it (swift3)


I want to be able to call function start within function use without hitting the action button for start. I know simple thing to do is just put print("a") in use. But I am using this as a simple example because I have a more complex problem in mind.

   @IBAction func start(_ sender: Any) {
print("a")}


fun use() {
}

viewdidload() {
use()
}

Solution

  • Create an IBOutlet for your button:

    @IBOutlet weak var button: UIButton!
    

    Then simply, use this code to trigger its action:

    button.sendActions(for: .touchUpInside)