When I study how to make button programmatically, I found to write UIControl.state
for setTitle and setTitleColor, and to write UIControl.event
for addTarget.
myButton.setTitle("This is button", for: .normal)
myButton.setTitleColor(.white, for: .normal)
myButton.backgroundColor = .darkGray
myButton.addTarget(self, action: #selector(btnTapped), for: .touchUpInside)
self.view.addSubview(myButton)
I wonder the relation between UIButton and UIControl.
Recently, for studying programmatic UI in swift, it is necessary to know UIKit hierarchy classes.
UIButton is child of ..
UIResponder -> UIView -> UIControl
Children of UIControl have special characteristics compare than childrens of UIView.
User can control visual UI changes, for example button can be clicked, and switch can be toggled.
UIControl supports it by Target-Action mechanism.
Now we can understand the definition on Apple developer documentation.
The base class for controls, which are visual elements that convey a specific action or intention in response to user interactions
From now on, let us study how Target-Action mechainsm support user interaction like above, and then deep dive into properties what I said.
UIControl.event
UIControl.state
properties make Target-Action mechanism working.
Target-Action Mechanism
Instead of writing code to track touch events, you write action methods to respond to control-specific events.
It would be supported by the method addTarget(_: action:for:)
.
class ViewController:UIViewController{
/*
code
*/
override func viewDidLoad(){
super.viewDidLoad()
/*
code
*/
myButton.addTarget(self, action: #selector(btnClicked),
for: .touchUpInside)
}
@objc func btnClicked(){
...
}
}
From this code, UIButton excutes myButton.addTarget(self, action: #selector(btnClicked),
for: .touchUpInside)
method which invokes action trough btnClicked()
As a result, if you add target method, and then action would be invoked when UIcontrol.event .touchUpInside
is satisfied.
Okay we do not know UIControl.event yet. let’s dive in.
UIControl.event
Developer can choose which event invoke actions from target.
For example, if you add .touchUpInside
(UIControl.event) on addTarget method, action will be invoke when you click inside of button.
Because there are so many kinds of UIControl.event, it will be better for you to find event which is adapt for your situation in apple document.
UIControl.state
Apple document definite it constants describing the state of a control.
Characteristics
- UIControl can have multiple states.
- User can set different actions depend on states.
It will be effective to understand with code.
For example, setTitle can be differ depend on states.
button.setTitle("Button1", for: .normal)
button.setTitle("Button2", for: .selected)
From this code, before select the button it shows “Button1” as title.
After select button, button shows “Button2” as title.
.normal
and .selected
is UIControl.state properties.
Especially, .normal
is default property.
When the control have .normal
property, other properties should be false.
The control which has .normal
cannot have multiple states.
As same as UIControl.event, because there are many other states, you can find those things from apple document as well.
In the end
By studying UIControl, I realize UIKit hierarchy really supports for my understanding.