I have a method which returns search results. I have a Runtime event listener which is used to monitor if the user has changed the search term, if the search term changes then:
- remove the previous results
- create a new results display group
- attempt to insert the new group into the scrollview
This seems to work when you enter the scene the first time. However, I’m getting a “Attempt to call method ‘insert’ (a nil value)” if I go out and come back in. I’ve made sure I’m removing all Runtime listeners in the destroy scene event and have verified the destroy scene event is getting called each time I leave the scene.
scene.lua (scrollview
local scrollView ... ... function scene:show( event ) local group = self.view ... scrollView = widget.newScrollView { top = 0, left = 0, width = display.contentWidth, height = display.contentHeight, isBounceEnabled = true, isLocked = false, horizontalScrollDisabled = true, backgroundColor = { 0, 0, 0, 0 }, listener = scrollListener } ... group:insert(scrollView) ... search.start(rY, scrollView)
search.lua
function search.resetResults( rY, scrollView ) -- resetResults gets called by search.start if user has changed the search term ... 154 resultGroup:removeSelf() 155 resultGroup = nil 156 resultGroup = display.newGroup() 157 resultGroup.y = rY 158 resultGroup.curY = 10 159 if scrollView and resultGroup then 160 print("resultGroup.y", resultGroup.y) 161 print("scrollView.\_widgetType", scrollView.\_widgetType) 162 scrollView:insert(resultGroup) 163 end
Note - I’ve added the if statement above to verify that both scrollView and the resultGroup exist before I try to insert resultGroup into the scrollView.
From the console:
resultGroup.y 936.97998046875
scrollView._widgetType scrollView
ERROR: Runtime error
?:0: attempt to call method ‘insert’ (a nil value)
stack traceback:
?: in function ‘insert’
search.lua:162: in function ‘resetResults’
search.lua:51: in function ‘?’
?: in function <?:190>
Using Corona Version 2018.3326 (2018.6.25)