As I'm creating a custom UITextField
, i.e. CurrencyTextField
as per How to input currency format on a text field (from right to left) using Swift?. I could set the configuration in willMove
function as below
override func willMove(toSuperview newSuperview: UIView?) {
Formatter.currency.locale = locale
addTarget(self, action: #selector(editingChanged), for: .editingChanged)
keyboardType = .numberPad
textAlignment = .right
sendActions(for: .editingChanged)
}
However, as I previously learn, we could also set it during init
as below instead of using willMove
override init(frame: CGRect) {
super.init(frame: frame)
Formatter.currency.locale = locale
addTarget(self, action: #selector(editingChanged), for: .editingChanged)
keyboardType = .numberPad
textAlignment = .right
sendActions(for: .editingChanged)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
They seems to behave the same for my case. So I'm not sure when should I use init
, and when should willMove
?
I saw this post on deinit
vs willMove
in Why is deinit not called until UIView is added to parent again?. That's well explained for deinit
vs willMove
, but not clear my question on init
vs willMove