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
Where to Find a Clear Explanation About Swift Alert (Uialertcontroller)
Add Gps Metadata Dictionary to Image Taken with Avfoundation in Swift
In Swift, Can a Function Be a Type
Firebase Get Child Id Swift iOS
Exc_Bad_Access Using Ibinspectable
Swift 4 Uicollectionview Detect End of Scrolling
Create Navbar Programmatically with Button and Title Swift
Automatically Change Cell Height Based on Content - Swift
Replaykit Startrecording Sometimes Never Enters Completion Handler
How to Use a Uisplitviewcontroller in Swift
Avmutablevideocomposition Output Video Shrinked
Read Logs Using the New Swift Os_Log API
Apple Push Notifications in Tvos
Removing a View Controller from Memory When Instantiating a New View Controller
Why Sending Message from Watchkit Extension to iOS and Getting Back a Reply Is So Slow
Xcode 8 How to Show Description of Function While Typing
How to Simultaneously Satisfy Constraints with Keyboard and Uitoolbar
How to Store JSON Decodable Values into Coredata Using Swift