Component - Button and action handling
This UI component shows a button that the user can tap. When tapped, it changes the text on the screen. It teaches how to connect a button to an action in Swift for iOS apps.
This UI component shows a button that the user can tap. When tapped, it changes the text on the screen. It teaches how to connect a button to an action in Swift for iOS apps.
UIViewController
└── UIView (main view)
├── UILabel
└── UIButtonlet button = UIButton()
button.addTarget(self, action: #selector(showMessage), for: .touchUpInside)
@objc func showMessage() {
print("Button was tapped")
}let button = UIButton()
button.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)
func buttonPressed() {
print("Pressed")
}let button = UIButton()
button.setTitle("Tap me", for: .normal)
button.addTarget(self, action: #selector(changeTitle), for: .touchUpInside)
@objc func changeTitle() {
// What goes here?
}Pass the test and this pattern moves unknown