Search code examples
iosreactive-cocoa

ReactiveCocoa: How do you bind a button's title to a text property?


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.


Solution

  • 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];
    }];