/ / Kann man im Property Setter einen Wert erzwingen? - iPhone, Ziel-C, uinavigationbar

Ist es möglich, einen Wert im Eigenschaftssetzer zu erzwingen? - iPhone, Ziel-C, uinavigationbar

Ich möchte UINavigationBar tintColor setter überschreiben und eine Standardfarbe erzwingen. Bellow ist mein Code (was natürlich fehlschlägt). Gibt es eine Möglichkeit, damit es funktioniert?

@implementation UINavigationBar (UINavigationBarCategory)
- (void)setTintColor:(UIColor *)tint {
self.tintColor = [UIColor greenColor];
}
@end

Antworten:

2 für die Antwort № 1

(Ich benutze property als Oberbegriff hier, in Ihrem Beispiel ist es ein Stand-In für tintColor)

Ich glaube nicht, dass Sie das benutzen können self.property = Syntax für die Zuordnung innerhalb eines setProperty: Methode. self.property ist nur ein Alias ​​für [self setProperty:<value>] und es wird sich rekursiv anrufen.

Sie müssen so etwas tun:

- (void)setProperty:(id)pProperty {
[property autorelease];

property = [pProperty retain];
}

Ich bin immer noch nicht 100% sicher, was Sie tun wollen, aber das Vorhergehende ist ein Anfang.


1 für die Antwort № 2

Wenn Sie eine Standardfarbe einer Navigationsleiste erzwingen möchten, warum legen Sie nicht die Farbtonfarbe in viewDidLoad / viewDidAppear Ihres View-Controllers fest?

self.navigationController.navigationBar.tintColor = [UIColor (color you want)];