Closed
Description
If I set text on an NSTextField
programatically via -setStringValue:
or -setAttributedStringValue:
, this doesn't trigger NSControlTextDidChangeNotification
, which means that -rac_textSignal
won't pick up the change either. It only works when the text is being edited by user interaction.
Easy fix would be to use -rac_signalForSelector:
to intercept calls to the setters as well (in addition to registering for NSControlTextDidChangeNotification
). Any other ideas?
Activity
jspahrsummers commentedon Aug 27, 2013
The consistency with AppKit here is a feature, not a bug. It would be weird, IMO, for
rac_textSignal
to fire for events that are not conventionally observable.This doesn't always make sense, but generally events that propagate to the view layer should be considered "final." If you need to filter or transform them before that point, it should be done through a view model or something along those lines.
What's the use case for observing programmatic changes?
indragiek commentedon Aug 27, 2013
Specific example: I have a form with several text fields and a Clear button, clicking Clear sets
stringValue
on every text field to@""
. I userac_textSignal
for some form validation logic, and the validation status isn't updated when Clear is clicked because-rac_textSignal
doesn't pick up the changes. The only workaround is to manually postNSControlTextDidChangeNotification
, which is a horrible thing to dojspahrsummers commentedon Aug 27, 2013
In terms of MVVM, the correct design would be to perform validation upon the view model, not the view. The view model would have two-way bindings to the text fields. Then, the Clear button can reset the fields of the view model, and everything is ✨.
MVC would be fairly similar, just with an intermediary view controller or model instead.
indragiek commentedon Aug 27, 2013
I had considered that, but using bindings is a problem on its own in my particular case. That's irrelevant to this question, however, so I think this can be closed. Thanks!
jspahrsummers commentedon Aug 27, 2013
FWIW, even if Cocoa Bindings™ is a problem, you can use
RACChannel
to build your own two-way bindings that are not reliant upon it.thebarndog commentedon Jun 14, 2014
I know this has been closed for a long time but I found a solution for your problem @indragiek.
Say you have a property,
NSString* name
on your viewModel.In your view controller, when binding the text field to the view model and vice versa do this:
[[RACSignal merge:@[self.nameField.rac_textSignal, RACObserve(self.nameField, text)]]
subscribeNext:^(NSString* text){
self.viewModel.name = text;
}];`
This way your detecting whenever the text signal fires AND for programatic changes to the text property.
TolyaAfanasev commentedon Dec 22, 2015
@startupthekid thanks, it helps for me.
kornerr commentedon Dec 26, 2016
@startupthekid thanks a ton!