7

I have an IBAction connected to a button, and I wanted to know if there is any way to run that function even if the button is not being pressed. This is what I tried...

Note: I am using swift for this project.

//I get an error when I type this code?
self.buttonPressed()

@IBAction func buttonPressed(sender: AnyObject) {

    print("Called Action")

}
Amit Singh
  • 2,698
  • 21
  • 49
nachshon fertel
  • 145
  • 1
  • 1
  • 11

6 Answers6

20

Make your sender argument optional and pass nil to ButtonPressed.

self.ButtonPressed( nil )


@IBAction func ButtonPressed( sender: AnyObject? ) {
    println("Called Action")
}
Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
Satachito
  • 5,838
  • 36
  • 43
10

One way would be to link your button up to its respective interface builder button and pass it into your function when you call it.

@IBOutlet weak var yourButton: UIButton!

self.buttonPressed(yourButton)

@IBAction func buttonPressed(sender: AnyObject) {
    print("Called Action")
}

Alternatively, define your function like so, then you'll be able to call your method the same way as you did before:

@IBAction func buttonPressed(sender: AnyObject? = nil) {
    print("Called Action")
}

// Call your method like this
self.buttonPressed()

// or this
self.buttonPressed(sender: nil)
Chackle
  • 2,249
  • 18
  • 34
  • @satachito Yeah, I'm really liking optional parameters in swift! Nice and clean code :) – Chackle May 01 '15 at 14:05
  • The '= nil' didn't work for me. What does that do exactly, btw? – NYC Tech Engineer Jul 22 '15 at 03:21
  • 2
    @NYCTechEngineer the '= nil' will only work if the type is defined as optional (the ? at the end of AnyObject). It basically means that if the method is called without any parameters it will by default set `sender` to nil. Does that make sense? – Chackle Jul 22 '15 at 08:10
  • @Chackle That actually makes more sense. I was trying to pass nil to the call, maybe that's why it wasn't working. All I have to do is call the function without passing any parameters, and it will automatically send in nil, right? – NYC Tech Engineer Jul 24 '15 at 15:34
  • @NYCTechEngineer That's exactly correct! :) Nice and clean, isn't it? – Chackle Jul 24 '15 at 15:37
  • @Chackle, it is pretty sweet, can't wait to try it out! – NYC Tech Engineer Jul 24 '15 at 15:39
2

Your ButtonPressed function needs an argument sender and yet you're not passing any when you call it. However, if you're doing this programatically, then you obviously don't have a sender.

Two ways to get around this:

  • Make the sender parameter an optional (AnyObject? instead of AnyObject) and invoke self.ButtonPress(nil). I just tried this and it works.
  • Put all the button press function in a separate function, e.g., performButtonPress(). Call it from inside the IBAction outlet, and if you want to press the button programatically, directly call performButtonPress().
Matthew Quiros
  • 13,385
  • 12
  • 87
  • 132
1

Call a button action using the following format: Swift 3

@IBAction func buttonSelected(_ sender: UIButton) {
        print("Butten clicked")
}

To call the button action:

let button = UIButton()
self.buttonSelected(button)
Ram Madhavan
  • 2,362
  • 1
  • 17
  • 20
0

You need to use "self" as "sender", example:

self.buttonPressed (sender: self)

or a simpler version

buttonPressed (sender: self)

Both tested in Swift 3

0
    lazy var Button : UIBarButtonItem = {
      let barButtonItem = UIBarButtonItem(title: "Button", style: .done, target: self, action: #selector(btnSelection))
      return barButtonItem
    }()


    func btnSelection(){
     // Code
    }

    // Now you can call self.btnSelection() whenever you want
Urvish Modi
  • 1,118
  • 10
  • 11