如何在新的分段控件中设置“id”?

时间:2014-03-25 08:58:08

标签: lua corona null

如何设置" id"在newSegmentedControl?

我已尝试过以下操作,但target.id始终为零。

    local function onSegmentPress( event )
        local target = event.target

        print( "Segment Label is:", target.segmentLabel )
        print( "Segment Number is:", target.segmentNumber )
        print( "id:", target.id )
    end

    local segmentedControl = widget.newSegmentedControl
    {
        id = "myID",
        left = 0,
        top = b.y - 13,
        segmentWidth = 60,
        segments = s,
        defaultSegment = 1,
        onPress = onSegmentPress
   }

1 个答案:

答案 0 :(得分:0)

看起来id是用于窗口小部件,而不是窗口小部件中的各个按钮,并且事件目标是实际按钮,它不包含id。

为处理程序提供ID的简便方法是创建一个闭包:

function genOnPressHandler(segmentId)
    return function (event)
        print("One of the buttons of Segment Control id=", segmentId, "was pressed")
        return true -- indicates successful touch
    end
end

local segment = widget.newSegmentedControl{
    id = "myID",
    left = 0,
    top = 50,
    segmentWidth = 60,
    segments = {"seg 1", "seg 2", "seg 3"},
    onPress = genOnPressHandler("myID") -- returns a closure that has ID of control
}