How to remove objects from scene when using tab bar on main page?

I am using a tab bar on the main.lua page and when you click a tab it loads a scene page with gotoScene. So when I click one tab and then another it does switch scenes but when I go back it starts acting funny and I get errors. I assume I need to use removeSelf on the objects and stuff. But I do not know where. I moved everything from the createScene to enterScene thinking that was an issue.

Here is the gotoScene code in the main page:

function GotoEventsList() if storyboard.page == "main" then logo:removeSelf() bar:removeSelf() end storyboard.removeAll() storyboard.gotoScene("sceneEventsList") return true end function GotoAreaList() if storyboard.page == "main" then logo:removeSelf() bar:removeSelf() end storyboard.removeAll() storyboard.gotoScene("sceneAreaList") return true end

I am using removeAll - does that purge the previous scenes? Any suggestions?

I only get an error if I select one tab, then another and then go back to the first tab. And when i select a row on that table view I get the error with the arithmetic of the contentheight error other people see.

Here is the code to the scene that gives me an error when I click a row after returning to it:

---------------------------------------------------------------------------------- -- -- sceneAreaList.lua -- ---------------------------------------------------------------------------------- local storyboard = require( "storyboard" ) local scene = storyboard.newScene() local widget = require( "widget" ) storyboard.page = "arealist" local sqlite3 = require "sqlite3" local data = {} local list local bar --Handle the applicationExit event to close the db local function onSystemEvent( event ) if( event.type == "applicationExit" ) then db:close() end end --Setup the table if it doesn't exist --local tablesetup = [[CREATE TABLE IF NOT EXISTS nh (RowID INTEGER PRIMARY KEY, nhname, nhdesc, nhpicture, nhthumbnail, areaname, lat, lon);]] --print(tablesetup) --db:exec( tablesetup ) -- create a constant for the left spacing of the row content local LEFT\_PADDING = 10 local halfW = display.contentCenterX local halfH = display.contentCenterY --Handle the applicationExit event to close the db local function onSystemEvent( event ) if( event.type == "applicationExit" ) then db:close() end end --Set the background to white display.setDefault( "background", 0/0 ) --Create a group to hold our widgets & images local widgetGroup = display.newGroup() --setup the system listener to catch applicationExit Runtime:addEventListener( "system", onSystemEvent ) local function onRowTouch( event ) local phase = event.phase if ( "press" == phase ) then print( "Goto " .. data[event.target.index].title ) storyboard.area = data[event.target.index].title print( "Area Touched row:", event.target.index ) list:removeSelf() bar:removeSelf() storyboard.removeAll() storyboard.gotoScene("sceneArea2") end end -- Handle row rendering local function onRowRender( event ) local phase = event.phase local row = event.row -- in graphics 2.0, the group contentWidth / contentHeight are initially 0, and expand once elements are inserted into the group. -- in order to use contentHeight properly, we cache the variable before inserting objects into the group local groupContentHeight = row.contentHeight local Title = data[row.index].title local rowTitle = display.newText( row, Title, 0, 0, native.systemFontBold, 18 ) -- in Graphics 2.0, the row.x is the center of the row, no longer the top left. rowTitle.x = LEFT\_PADDING rowTitle.x = 85 rowTitle.anchorX = 0 rowTitle.y = groupContentHeight \* 0.5 rowTitle:setFillColor( 0, 0, 0 ) local rowImage rowImage = display.newImageRect(row, data[row.index].image, 69, 70) rowImage.anchorX = 0 rowImage.anchorY = 0 rowImage.x = 3 rowImage.y = 6 local rowArrow = display.newImage( row, "images/rowArrow.png", false ) rowArrow.x = row.contentWidth - LEFT\_PADDING -- we set the image anchorX to 1, so the object is x-anchored at the right rowArrow.anchorX = 1 rowArrow.y = groupContentHeight \* 0.5 end -- Called when the scene's view does not exist: function scene:createScene( event ) local group = self.view end -- Called immediately after scene has moved onscreen: function scene:enterScene( event ) local group = self.view --Open data.db. If the file doesn't exist it will be created local path = system.pathForFile("abroad.db", system.ResourceDirectory) db = sqlite3.open( path ) local ct = 1 for row in db:nrows("SELECT \* FROM area") do --local t = display.newText(row.content, 20, 30 \* row.id, null, 16) --t:setFillColor( 1, 0, 1 ) print( row.areaname ) data[ct] = {} data[ct].title = row.areaname data[ct].subtitle = "" data[ct].image = "images/" .. row.areathumbnail ct = ct + 1 end display.setStatusBar(display.DefaultStatusBar) bar = display.newImageRect("images/topbarnightlife.png", 320, 50) bar.anchorX = 0 bar.anchorY = 0 bar.x = 0 bar.y = 0 -- Create a tableView list = widget.newTableView { top = 50, width = 320, height = 380, maskFile = "mask-320x448.png", onRowRender = onRowRender, onRowTouch = onRowTouch, } --Insert widgets/images into a group widgetGroup:insert( list ) for i=1,#data do list:insertRow{ rowHeight = 72, } end end -- Called when scene is about to move offscreen: function scene:exitScene( event ) local group = self.view ----------------------------------------------------------------------------- -- INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.) ----------------------------------------------------------------------------- end -- Called prior to the removal of scene's "view" (display group) function scene:destroyScene( event ) local group = self.view ----------------------------------------------------------------------------- -- INSERT code here (e.g. remove listeners, widgets, save state, etc.) ----------------------------------------------------------------------------- end --------------------------------------------------------------------------------- -- END OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- -- "createScene" event is dispatched if scene's view does not exist scene:addEventListener( "createScene", scene ) -- "enterScene" event is dispatched whenever scene transition has finished scene:addEventListener( "enterScene", scene ) -- "exitScene" event is dispatched before next scene's transition begins scene:addEventListener( "exitScene", scene ) -- "destroyScene" event is dispatched before view is unloaded, which can be -- automatically unloaded in low memory situations, or explicitly via a call to -- storyboard.purgeScene() or storyboard.removeScene(). scene:addEventListener( "destroyScene", scene ) --------------------------------------------------------------------------------- return scene

If you call storyboard.removeAll() in your exitScene function I believe that will solve your problem.

That did not fix it. Same error.

I would look at the Business App Sample on our github repository https://github.com/coronalabs/business-app-sample

and see how I structured it.  I’m doing a storyboard.removeAll() in the button handler like @euporiacorona suggested, but I think you’re not getting your storyboard scene objects inserted into the scene’s view (group) correctly.  If objects are not inserted into “group”, they won’t be managed by storyboard.

The Business App Sample will show you how to structure your storyboard scenes.

Rob

If you call storyboard.removeAll() in your exitScene function I believe that will solve your problem.

That did not fix it. Same error.

I would look at the Business App Sample on our github repository https://github.com/coronalabs/business-app-sample

and see how I structured it.  I’m doing a storyboard.removeAll() in the button handler like @euporiacorona suggested, but I think you’re not getting your storyboard scene objects inserted into the scene’s view (group) correctly.  If objects are not inserted into “group”, they won’t be managed by storyboard.

The Business App Sample will show you how to structure your storyboard scenes.

Rob