How to Auto Call an @Ibaction Function

How to Auto call an @IBAction function

You can either change the signature of the IBAction by making its parameter an Optional like this:

@IBAction func doSomeTask(sender: UIButton?) {
// code
}

and then call it with nil as an argument:

doSomeTask(nil)

Or you can use the IBAction as a wrapper for the real function:

func doSomeTaskForButton() {
// ...
}

@IBAction func doSomeTask(sender: UIButton) {
doSomeTaskForButton()
}

meaning you can then call doSomeTaskForButton() from wherever you want.

Swift - Calling @IBAction from another method without parameter

An IBAction receives an event from the interface builder. What you are looking for is an IBOutlet which ties the UI element to a variable. Control drag button1 (that is: hold down ctrl and then drag) from the interface builder to the body of your navigation controller to create an IBOutlet and change it's state in the customPage function.

What is the purpose of under score in when declaring a IBAction func?

This place is declared for the label which means that once you call the function it won't appear to you for example:

   func sum (_ number1: Int, _ number2: Int) {
print(number1 + number2)
}

once you call the function you won't need to mention number1 or number2 but you will only write the number directly :

sum(1, 2)

To be clear it's the same as using the function below:

func summation(myFirstNumber number1: Int, mySecondNumber number2: Int) {
print (number1 + number2)
}

but here instead of using _ I've used a label so when I called the function, I will use these labels :

summation(myFirstNumber: 1, mySecondNumber: 2)

So it's clear now that _ is instead of writing a label.

For more information check: Function Argument Labels and Parameter Names from here

Swift: How to access one object (ie. a button) within a @IBAction collection to return a sender to a new function?

Create a function that receives an UIButton as parameter with your animation code inside:

func animateButton(anySender: UIButton) {
// Your animation code here
sender.transform = CGAffineTransform(scaleX: 1.2, y: 1.2) // completion handler to reverse the animation
}

Call your animation in any function that requires the animation, passing your sender as parameter:

@IBAction func numberButtonTapped(_ sender: UIButton) {
guard let numberButton = sender.title(for: .normal) else {return}

animateButton(anySender: sender)
updateInput(num: numberButton)
}

EXTRA: if you need to call this function from different controllers, consider making it an extension:

extension UIButton {
func customAnimate() {
// Your animation code here
self.transform = CGAffineTransform(scaleX: 1.2, y: 1.2) // completion handler to reverse the animation
}
}

and call it from any button:

@IBAction func numberButtonTapped(_ sender: UIButton) {
guard let numberButton = sender.title(for: .normal) else {return}

sender.customAnimate()
updateInput(num: numberButton)
}


Related Topics



Leave a reply



Submit