Ich habe ein Protokoll wieUIButton background Eigenschaft mit Protokoll
protocol ButtonPresentable {
var backgroundColor: UIColor { get }
}
extension ButtonPresentable {
var backgroundColor: UIColor? {
return UIColor.red
}
}
ich einen Knopf benutzerdefinierte Klasse haben (ziehen mit IB), die mein Protokoll implementiert
class MyButton: UIButton, ButtonPresentable {
}
Warum dieses Protokoll nicht " override 'native backgroundColor-Eigenschaft?
Ich möchte eine Standardprotokollimplementierung haben, die einen Standardhintergrund für alle Knöpfe setzt. Gibt es eine protokollorientierte Möglichkeit, Standardwerte für alle UIButton-Hintergrundfarben festzulegen?
EDIT: Eigentlich mache ich die
protocol ButtonPresentable {
var pBackgroundColor: UIColor { get }
}
extension ButtonPresentable {
var pBackgroundColor: UIColor? {
return UIColor.red
}
func applyTheme() {
self.backgroundColor = pBackgroundColor
}
}
// In my custom view
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
self.applyTheme()
}
hinzufügen 'override' Stichwort vor 'var' Deklaration. Hast du das getestet? – Adolfo
@ Adolfo Nein! Hat nicht funktioniert – Ludovic