Reputation: 7938
I've got a UIButton whose title for state normal/highlighted should keep synced with a property of its container object.
How can I bind button title for specific state with a string property?
Edit:
I know that using RACObserve and change the button title in subcribeNext block is a solution.
I'm looking for something more specifically designed for UIButton like:
RACBindButtonTitle(button, property, state1, state2);
I don't know if there is some "RAC sugar" like this.
Upvotes: 2
Views: 1605
Reputation: 3387
Please refer to this gist.
You can't use RAC()
directly on a UIButton because of the UIKit design:
RAC(self.button.titleLabel, text) = titleSignal; // Don't do this.
One of the solutions is using dynamic property to support RAC()
binding macro:
// .h
@interface UIButton (RACTitleSupport)
@property (strong, nonatomic) NSString *racExt_Title;
@end
// .m
@implementation UIButton (RACTitleSupport)
@dynamic racExt_Title;
- (void)setRacExt_Title:(NSString *)racExt_Title
{
[self setTitle:racExt_Title forState:UIControlStateNormal];
}
- (NSString *)racExt_Title
{
return [self titleForState:UIControlStateNormal];
}
@end
Now you can use RAC()
binding macro like this:
RAC(self, button.racExt_Title) = titleSignal;
Cheers <3
Upvotes: 0
Reputation: 2195
Here's a way to do it without explicit subscription.
Explicit subscription should be avoided whenever possible so that you don't have to go through the whole @weakify(self)
@strongify(self)
dance.
[self.button rac_liftSelector:@selector(setTitle:forState:)
withSignals:
RACObserve(self, normalButtonTitle),
[RACSignal return:@(UIControlStateNormal)],
nil];
[self.button rac_liftSelector:@selector(setTitle:forState:)
withSignals:
RACObserve(self, selectedButtonTitle),
[RACSignal return:@(UIControlStateSelected)],
nil];
liftSelector:withSignals:
will subscribe eagerly to its signals, unlike many another RAC functions.
Upvotes: 6
Reputation: 3534
If you mean they're synced with one property, something like this:
[RACAble(self.buttonTitle) subscribeNext:^(NSString *newTitle) {
NSString *normalTitle = [NSString stringWithFormat:@"Normal %@", newTitle];
NSString *highlightedTitle = [NSString stringWithFormat:@"Highlighted %@", newTitle];
[self.button setTitle:normalTitle forState:UIControlStateNormal];
[self.button setTitle:highlightedTitle forState:UIControlStateHighlighted];
}];
If you mean there're two properties, something like this:
[RACAble(self.normalButtonTitle) subscribeNext:^(NSString *newTitle) {
[self.button setTitle:newTitle forState:UIControlStateNormal];
}];
[RACAble(self.highlightedButtonTitle) subscribeNext:^(NSString *newTitle) {
[self.button setTitle:newTitle forState:UIControlStateHighlighted];
}];
Upvotes: 1