when ButtonView.Click:
initialize local displayText to ""
for each item in global shoppingList:
set displayText to displayText & item
set LabelDisplay.Text to displayText
这里有两个核心积木:
- get(取值):读取变量的当前值
- set(设置值):给变量赋新值
看似简单,但有个陷阱:全局变量用 set 会直接覆盖。如果你想累加,必须写成 set global X to get X + 1,而不是 set global X to 1。
Step 4:持久化——变量的"存档"机制
全局变量在App关闭后就消失了。要实现"关闭再打开数据还在",需要TinyDB配合:
when Screen1.Initialize:
set global shoppingList to TinyDB1.GetValue(tag: "shoppingList", default: create empty list)
set global totalCount to TinyDB1.GetValue(tag: "count", default: 0)
when ButtonSave.Click:
call TinyDB1.StoreValue(tag: "shoppingList", value: global shoppingList)
call TinyDB1.StoreValue(tag: "count", value: global totalCount)