在加载远程图像后,将ID添加到监听器。

我有一个用于加载远程图像的监听器,但我需要能够将一个 ID 号传递给该监听器,但我不太确定如何实现。我的获取远程图像的代码如下:

display.loadRemoteImage("http://www.newyorker.com/online/blogs/photobooth/NASAEarth-01.jpg", "GET", networkListener, "banner.png",system.TemporaryDirectory,  (globalData.contentX * rows2) + globalData.contentX/2, 20 + (i - 1) % 6 * 140

我现在有一个监听器:

local function networkListener( event )
    if ( event.isError ) then
        print ( "Network error - download failed" )
    else
        local target = event.target
        target.alpha = 0
        transition.to( target, { alpha = 1.0 } )
        target.width = 590
        target.height = 110
        target:addEventListener( "touch", target )
        scrollView:insert(target)
        function target:touch(event)
            if event.phase == "began" then
                display.getCurrentStage():setFocus( self )
                self.isFocus = true

            elseif self.isFocus then
                if event.phase == "moved" then
                    numMoved = numMoved + 1
                    if(numMoved > 10) then
                        display.getCurrentStage():setFocus( nil )
                        self.isFocus = false
                        scrollView:takeFocus( event )
                    end
                elseif event.phase == "ended" or event.phase == "cancelled" then
                    globalData.selectedLocationID = target.id --我需要将 ID 传递给此监听器。
                    if(globalData.approvedToggle == 1) then
                        storyboard.gotoScene("businessScene")
                    else
                        storyboard.gotoScene("locationScene")
                    end
                    display.getCurrentStage():setFocus( nil )
                    self.isFocus = false
                end
            end
        return true
    end
end

如有任何帮助,不胜感激,谢谢!

点赞
用户825481
用户825481

我之前在一个电商应用上完成过类似的操作。我不太熟悉 storyboard,但我记得使用了这个 API。如果我没记错的话,你需要将 event.target 作为事件监听器并将所有参数传递过去。我还记得你可以在 display.loadRemoteImage API 中嵌入一个函数,像这样:

itemImage = display.loadRemoteImage(itemData.imageURL, "GET",
            function(event)
               event.target.xScale = 0.4
               event.target.yScale = 0.4
               function openSite(event)
                   if event.phase == "ended" then
                      system.openURL( itemData.itemURL )
                   end
               end
               event.target:addEventListener( "tap", openSite )
            end)

我的建议是删除所有 storyboard 的东西,尝试在一个不同的文档中使用 API。我认为你需要简化它,以免混淆。

希望能有所帮助。

2013-12-12 21:29:29