Search code examples
iosobjective-creactive-cocoa

enabled property from UIBarButtonItem does not take effect when a RACCommand is defined


In the snipped bellow, I want to change the enableness status of self.btnSave, that has a RACCommand defined. The second block actually tries to change it based on another external condition (tableview selection).

It seams that the rac_command takes control of the enabled status of the button, and the only way to make it work, would be creating a signal to control the enable status (that I have no idea how to do it)

Do you have any idea? I would like to keep using RAC for the button actions, but if it gets more complicated than I imagined, I will have to abandon it.

Thanks in advance.

@weakify(self);
self.btnSave.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
    @strongify(self);
    Album* lalbum = [NSEntityDescription
               insertNewObjectForEntityForName:@"Album"
               inManagedObjectContext:self.production.managedObjectContext];

    lalbum.name = self.txtAlbumName.text;
    [self.production addAlbunsObject: lalbum];

    [self.production.managedObjectContext save: nil];

    self.close();

    return [RACSignal empty];
}];

[[self.txtAlbumName rac_textSignal] subscribeNext:^(NSString* text) {
    @strongify(self);
    if ([text isEqualToString:@""])
    {
        self.btnSave.enabled = NO;
    } else
    self.btnSave.enabled = YES;
}];

Solution

  • The point of RACCommand is that it lets you disable the button while you perform some long-running action. If you don't want that (and your question implies that you don't), just use normal target/action semantics. There are other libraries that give you nice block-based helpers for UIBarButtonItems, if that's what you're after.

    Also, try this on for size:

    RAC(self.btnSave, enabled) = [self.txtAlbumName.rac_textSignal map:^(NSString *text) {
        return ![text isEqualToString:@""];
    }];