Skip to content

Instantly share code, notes, and snippets.

@xareelee
Last active November 25, 2016 03:56
Show Gist options
  • Save xareelee/55c9a965212ebab524c8 to your computer and use it in GitHub Desktop.
Save xareelee/55c9a965212ebab524c8 to your computer and use it in GitHub Desktop.
Use RAC() macro for binding a UIButton title

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment