Reactive Cocoa - 以编程方式设置文本时不会调用UITextView的rac_textSignal [英] Reactive Cocoa - UITextView's rac_textSignal doesn't get called when programmatically setting text

查看:490
本文介绍了Reactive Cocoa - 以编程方式设置文本时不会调用UITextView的rac_textSignal的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实施聊天用户界面,并使用Reactive Cocoa根据用户的类型调整聊天气泡的大小。目前,我正在根据textview的 rac_textSignal 更新UI的布局。一切都很好 - 除了一点:当用户发送消息时,我以编程方式清除文本字段:

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 = @"";

...但是textview的 rac_textSignal 没有不要激活。 我听说这是ReactiveCocoa的一个功能 - 但是构建它的正确方法是什么?我是否需要拥有一个持有 currentlyTypedString 的NSString,并在该字符串更新时驱动UI更改?

... 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?

推荐答案

是的,这是正确的。

MVVM 视图模型应被视为UI数据和事件的规范来源(这会带来一系列重要的好处,就像更好的可测试性)。您将在视图模型上存储键入的 NSString ,然后将 绑定到UI。

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.

使用MVC,您必须使用控制器或模型,但原则是相同的:将视图视为瞬态数据并在其他地方执行重要操作。

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.

这篇关于Reactive Cocoa - 以编程方式设置文本时不会调用UITextView的rac_textSignal的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆