私は、RegistrationButtonというカスタムUIButtonを作成しました。私は現在、ユーザーが3つのボタンの中から選択できるようにしようとしています。ボタンを選択すると背景色とテキストが変わります。ここ はIBActionがどのように見えるかです:私は1つのボタンを上の選択した場合しかし、すべての背景がに変更されIBActionで登録されたカスタムUIButtonの色を変更するにはどうすればよいですか?
import UIKit
class RegistrationButton: UIButton {
override func awakeFromNib() {
super.awakeFromNib()
addBorder()
}
required init(coder:NSCoder) {
super.init(coder:coder)!
}
func addBorder(){
self.layer.borderColor = UIColor.white.cgColor
self.layer.borderWidth = 1
}
func selected(){
self.backgroundColor = .white
self.setTitleColor(UIColor(red:67,green:204,blue:144,alpha:1), for: .normal)
}
func notSelected(){
self.backgroundColor = UIColor(red:67,green:204,blue:144,alpha:1)
self.setTitleColor(UIColor.white, for: .normal)
}
}
:ここ
@IBAction func didTapBUtton(_ sender: UIButton) {
switch sender {
case studentButton:
studentButton.selected()
professionalButton.notSelected()
entrepreneurhsip.notSelected()
break
case professionalButton:
studentButton.notSelected()
professionalButton.selected()
entrepreneurhsip.notSelected()
break
case entrepreneurhsipButton:
studentButton.notSelected()
professionalButton.notSelected()
entrepreneurhsip.selected()
break
default:
break
}
}
は、どのように私のカスタムUIButtonクラスです白。
:
またはあなたはスウィフトでdidSetメカニズムを利用することができます。物事のベースラインの理由が質問者であっても、UIButtonを使用する方法を理解できず、必要に応じてクラスを拡張することはほとんどありません。 – dfd