Consegui criar o botão, porém queria entender qual a melhor maneira de você adicionar o target. No meu caso eu fiz semelhante as aulas anteriores de criar um delegate do componente e implementar o delegate na view pai que chama o componente, porém fiquei na dúvida se isso seria o correto, já que não estou conseguindo fazer funcionar. Existe uma maneira melhor para esse caso?
protocol APButtonDelegate: AnyObject {
func didButtonTap(_ sender: APButton)
}
class APButton: UIView {
var buttonText: String
weak var delegate: APButtonDelegate?
init(buttonText: String) {
self.buttonText = buttonText
super.init(frame: .zero)
setupView()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
lazy var button: UIButton = {
let button = UIButton()
button.translatesAutoresizingMaskIntoConstraints = false
button.backgroundColor = UIColor(named: "ColorCoral")
button.titleLabel?.font = .init(name: "Poppins-Bold", size: 18)
button.layer.cornerRadius = 8
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
return button
}()
private func setupView(){
updateUI()
addSubviews()
setupConstraints()
}
private func updateUI(){
button.setTitle(buttonText, for: .normal)
}
private func addSubviews(){
addSubview(button)
}
private func setupConstraints(){
NSLayoutConstraint.activate([
button.topAnchor.constraint(equalTo: topAnchor),
button.leadingAnchor.constraint(equalTo: leadingAnchor),
button.trailingAnchor.constraint(equalTo: trailingAnchor),
button.heightAnchor.constraint(equalToConstant: 48),
])
}
@objc private func buttonTapped(){
print("Clicou o botão")
delegate?.didButtonTap(self)
}
}
Criei o botão da seguinte forma e declarei assim no pai:
private lazy var signInButton: APButton = {
let buttonComponent = APButton(buttonText: "Entrar")
buttonComponent.translatesAutoresizingMaskIntoConstraints = false
buttonComponent.delegate = self
return buttonComponent
}()
//....... Constraints
NSLayoutConstraint.activate([
shape2.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -48),
logo.centerXAnchor.constraint(equalTo: view.centerXAnchor),
logo.topAnchor.constraint(equalTo: shape.bottomAnchor, constant: -108),
text.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 32),
text.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -32),
text.topAnchor.constraint(equalTo: logo.bottomAnchor, constant: 32),
paws.trailingAnchor.constraint(equalTo: view.trailingAnchor),
stack.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 32),
stack.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -32),
stack.topAnchor.constraint(equalTo: text.bottomAnchor, constant: 32),
emailTxtField.heightAnchor.constraint(equalToConstant: 48),
passwordTxtField.heightAnchor.constraint(equalToConstant: 48),
signInButton.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 88),
signInButton.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -88),
signInButton.topAnchor.constraint(equalTo: stack.bottomAnchor, constant: 32),
])
//............... Extension
extension SignInViewController: APButtonDelegate {
func didButtonTap(_ sender: APButton) {
signIn()
}
}
Mesmo assim parece que ele não recebe o clique, não sei pq