Reactive Cocoa - UITextView's rac_textSignal doesn't get called when programmatically setting text
Asked Answered
R

3

7

I'm implementing a chat UI, and using Reactive Cocoa to adjust the chat bubble's size as the user types. Currently, I'm updating the UI's layout based on the textview's rac_textSignal. Everything's working great - except for one bit: when the user sends the message, I programmatically clear the textfield:

_inputTextView.text = @"";

... but the textview's rac_textSignal doesn't activate. I hear this is a feature with ReactiveCocoa - but what's the proper way to build this? Do I need to have an NSString holding the currentlyTypedString, and drive the UI changes when that string updates?

Rodi answered 25/11, 2013 at 7:43 Comment(1)
I know this question is kind of old but if you just use RACSignal.combineLatest([self.textView.rac_textSignal(), RACObserve(self.textView, "text")] yadda yadda yadda. That way you subscribe to both UI and programatic updatesOzan
V
7

Yep, that's correct.

Under MVVM, the view model should be considered the canonical source of UI data and events (which leads to a whole host of important benefits, like better testability). You'd store the typed NSString on the view model, then bind that to the UI.

With MVC, you'd have to use the controller or model instead, but the principle is the same: treat the view as transient data and do the important stuff elsewhere.

Valeda answered 25/11, 2013 at 8:1 Comment(0)
T
10

Just send action:

[self.inputTextView sendActionsForControlEvents:UIControlEventEditingChanged];

Tingaling answered 3/3, 2015 at 12:32 Comment(0)
V
7

Yep, that's correct.

Under MVVM, the view model should be considered the canonical source of UI data and events (which leads to a whole host of important benefits, like better testability). You'd store the typed NSString on the view model, then bind that to the UI.

With MVC, you'd have to use the controller or model instead, but the principle is the same: treat the view as transient data and do the important stuff elsewhere.

Valeda answered 25/11, 2013 at 8:1 Comment(0)
C
0

The following is a workaround that works:

[[RACSignal 
  merge:@[self.inputTextView.rac_textSignal, RACObserve(self.inputTextView, text)]] 
  subscribeNext:^(NSString* text) {
      // do something here
  }];

Thanks to startupthekid on GitHub.

Collenecollet answered 22/12, 2016 at 13:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.