首页 > 解决方案 > 如何获取在 for 循环中创建的变量名

问题描述

基本上我有一个 for 循环,它根据数据库中的数据创建变量,然后我有一个事件侦听器,它也是基于 for 循环创建的,我想知道按下了哪个文本

我已经尝试了函数中的事件,为我的 row.name 等创建了一个变量。

for row in db:nrows( "SELECT * FROM Students WHERE Class = '"..class.."'" ) do
        print(row.Name)
        --track how many students there are 
        count = count+1
        --When displaying the names, put them in line, if they go below 1000 y level, move to the right and go down again
        ny = ny + 80
        if (ny == 1000) then
            nx = nx + 300
            ny = 280
        end
        -- Display students
        student[row] = display.newText( sceneGroup, row.Name, nx, ny, native.systemFont, 30 )
        --Make a button for every student in the row that goes to studentscene function
        student[row]:addEventListener( "tap", studentscene)
    end

然后函数看起来像

local function studentscene()
    composer.gotoScene( "student", { time=800, effect="crossFade" } )
end

我希望能够跟踪按下了哪个学生姓名,但我找不到这样做的方法。我需要这个,所以我可以在数据库中跟踪它的名称,这样我就可以显示他们的信息

标签: sqlsqliteluacoronasdkscene

解决方案


您可以通过在 goto 函数中使用参数来添加它

    `options = {
       effect = model,
       time = time,
       params = params,
    }
composer.gotoScene( nextScene , options )`

例如,这样做 params ={student="mohsen"} 并在场景:显示功能中这样做:

if event.params then

studen = event.params.name
end

推荐阅读