How to bind a LabelMorph/TextMorph to a variable so that the Morph reflects changes of the variable?

这一生的挚爱 提交于 2019-12-05 16:19:03

Depends on what you want to achieve. You might want to take a look at a way to do it with Glamour in a current Moose image. In a workspace, do-it:

GLMBasicExamples new magritte openOn: GLMMagrittePersonExample sampleData 

That shows how to work with announcements on save. The earlier examples are a better way to start understanding how to work with Glamour (and because of the way the examplebrowser is build, the Magritte example doesn't update the list when it is nested):

GLMBasicExamples open

That has several other examples that update on change.

Simplest is to use an updating String morph:

UpdatingStringMorph on: self selector: #myLabel

This will send #myLabel (or any other message) to self (or any other object) and display it.

This is a solution provided by Benjamin Van Ryseghem on the Pharo Mailinglist:

For this kind of situation, my solution is to use a ValueHolder. Instead of storing your string directly in an instance variable, store it into the ValueHolder.

I tried this in a Workspace:

|string label|

string := 'Wait till i change..' asValueHolder.
label := LabelMorph contents: string contents.
string whenChangedDo: [:newValue | label contents: newValue ].
label openInWindow.
[ 5 seconds asDelay wait. string value: 'I changed :)' ] fork. 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!