Hey Guys Today We will See About how we can use the closure based call on the button. Previously we were used to use the addtarget and selector method as used in objective C .
Whenever we used to use addtarget instead of IBAction we needed to have the selector and the function to call the button Action
button.addTarget(self,action: #selector(buttonTapped),for: .touchUpInside)
Due to which we need we needed to Add objc func to perform the Action
@objc func buttonTapped() {
count += 1
}
Now we no need to relay on selector instead of we can use the closure based Button call .
button.addAction(UIAction { [weak self ] _ in
self?.counter += 1
}, for: .touchUpInside)
Bellow example is for the selector function as we use previously
Class ViewController: UIViewController {
@IBOutlet weak var textLBL: UILabel!
@IBOutlet weak var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
button.addTarget(self,action: #selector(buttonTapped),for: .touchUpInside)
}
@objc func buttonTapped() {
count += 1
self.textLBL.text = count.description ?? ""
}
Now using the Closure in the button we can reduce the number of coding lines of the program
Class ViewController: UIViewController {
@IBOutlet weak var textLBL: UILabel!
@IBOutlet weak var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
button.addAction(UIAction { [weak self ] _ in
self?.count += 1
self?.textLBL.text = count.description ?? ""
}, for: .touchUpInside)
}
By Using UIKit closure button in swift
you can use closures with buttons to execute some code when the button is tapped. Here’s an example of how to use a closure with a button:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(type: .system)
button.setTitle("Tap me!", for: .normal)
button.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
view.addSubview(button)
// Add constraints for the button
button.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
button.centerXAnchor.constraint(equalTo: view.centerXAnchor),
button.centerYAnchor.constraint(equalTo: view.centerYAnchor),
])
}
@objc func buttonTapped(_ sender: UIButton) {
print("Button tapped!")
}
}
In this example, we create a button programmatically using the UIButton
class. We set the button’s title to “Tap me!” using the setTitle(_:for:)
method, and add a target and action using the addTarget(_:action:for:)
method. The target is self
, which means the current view controller, and the action is the buttonTapped(_:)
method that we define in the view controller. We also add the button as a subview of the view, and add constraints to center it in the view.
In the buttonTapped(_:)
method, we print a message to the console. This method gets called when the button is tapped, because we specified it as the action for the touchUpInside
event using the addTarget(_:action:for:)
method.
You can also use closures instead of a selector-based approach to handle button taps. Here’s an example of how to use a closure with a button:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(type: .system)
button.setTitle("Tap me!", for: .normal)
button.addAction(for: .touchUpInside) {
print("Button tapped!")
}
view.addSubview(button)
// Add constraints for the button
button.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
button.centerXAnchor.constraint(equalTo: view.centerXAnchor),
button.centerYAnchor.constraint(equalTo: view.centerYAnchor),
])
}
}
In this example, we create a button using the UIButton
class, set its title, and add it as a subview of the view with constraints, similar to the previous example. However, instead of using the addTarget(_:action:for:)
method, we use the addAction(for:_:)
method to add a closure that gets called when the button is tapped. We pass the .touchUpInside
event as the first argument, and a closure that prints a message to the console as the second argument.