Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NSControl -rac_textSignal doesn't work when programatically setting text #765

Closed
indragiek opened this issue Aug 27, 2013 · 8 comments
Closed
Labels

Comments

@indragiek
Copy link
Member

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?

@jspahrsummers
Copy link
Member

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
Copy link
Member Author

Specific example: I have a form with several text fields and a Clear button, clicking Clear sets stringValue on every text field to @"". I use rac_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 post NSControlTextDidChangeNotification, which is a horrible thing to do :trollface:

@jspahrsummers
Copy link
Member

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
Copy link
Member Author

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
Copy link
Member

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
Copy link

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
Copy link

@startupthekid thanks, it helps for me.

@kornerr
Copy link

kornerr commented Dec 26, 2016

@startupthekid thanks a ton!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants