我将 Lua 5.1 与 IUP 3.5 一起使用,并尝试使用列表回调来根据所选地点填充地址列表。(列表是一个编辑框,所以我需要在适当的时候处理它,但让我们先处理基础知识)。我显然对如何做到这一点有一个基本的误解。
编码:
function MakeAnIupBox
--make some more elements here
listPlace = iup.list{}
listPlace.sort = "YES"
listPlace.dropdown = "YES"
--populate the list here
--now handle callbacks
listPlace.action = function(self) PlaceAction(text, item, state) end
end
function PlaceAction(text, item, state)
listAddress.REMOVEITEM = "ALL"
if state == 1 then -- a place has been selected
--code here to populate the Addresses list
end
end
iup文档将列表的操作回调描述为
ih:action(text: string, item, state: number) -> (ret: number) [in Lua]
但是,当我运行此代码时,我得到:
- text -- 看起来像是某种元表
- 项目,状态——都为零
我也尝试将回调编码为
function MakeAnIupBox
--make some more elements here
listPlace = iup.list{}
listPlace.sort = "YES"
listPlace.dropdown = "YES"
--populate the list here
end
function listPlace:action (text, item, state)
listAddress.REMOVEITEM = "ALL"
if state == 1 then -- a place has been selected
--code here to populate the Addresses list
end
end
但这无法运行:错误是attempt to index global 'listPlace' (a nil value)
我不想在“MakeAnIupBox”中嵌入回调,因为我希望在几个 Lua 程序中使其(和其他相关的回调)成为一个可重复使用的组件,这些程序都处理类似的数据集,但来自不同的 UI。