tabbar initialized in main.lua - how to reference it in another scene to :setSelected() ?

Dear corona community,

I set up a tabbar in main.lua:

-- create menu local tabBar = widget.newTabBar( { top = display.screenOriginY + display.actualContentHeight - 50, [...] } )

which works basically pretty well. Problem is: Within one of my scenes, I have an overlay with a

composer.gotoScene(“abc”) inside the scene:hide function. So, when the overlay hides, scenery changes automatically. The scene switch works perfectly, unfortunately the tabbar does not get updated and still shows the old scene as active.

So, the question is: How do I reference the tabbar within that overlay (or within scene “abc”) to activate the desired button?

Thank you

Update: I made it work using a seperate globals.lua storage file:

---------------------------------------------------------------------------------------------- -- globals file that stores information and data that can be accessed from anywhere in the app ---------------------------------------------------------------------------------------------- -- table that holds information and data local globalSpace = {} ---------------------------------------------- -- forward declare variables within that table ---------------------------------------------- local tabbar -- stores the tabbar ------------------- -- internal setter functions ------------------- local function setTabbar(tbbr) tabbar = tbbr end ------------------ -- internal getter functions ------------------- local function getterTabbar() return tabbar end ------------------------- -- set external functions ------------------------- globalSpace.setTabbar = setTabbar globalSpace.getTabbar = getterTabbar -- return the table return globalSpace

In my main.lua, I do
globalSpace.setTabbar(tabBar)

and in my scene, I do

local tabbar = globalSpace.getTabbar()
tabbar:setSelected(3)

it works, but is it also a sensible solution?

Hi @christoph-graf,

Yes, if you make a “global storage” as outlined in the Goodbye Globals! tutorial, then your method is perfectly valid.

Using Composer, you could also use the “setVariable()” and “getVariable()” functions to store a reference to the tab bar and access it from other scenes/overlays:

composer.setVariable()

composer.getVariable()

Take care,

Brent

Thank you Brent Sorrentino for going through my code. Appreciate your help and yes, the “Goodbye Globals” tutorial helped a lot. Cool, so Ill leave it the way it is, although the setVariable() and getVariable() sounds cool, too.

Cheers

Update: I made it work using a seperate globals.lua storage file:

---------------------------------------------------------------------------------------------- -- globals file that stores information and data that can be accessed from anywhere in the app ---------------------------------------------------------------------------------------------- -- table that holds information and data local globalSpace = {} ---------------------------------------------- -- forward declare variables within that table ---------------------------------------------- local tabbar -- stores the tabbar ------------------- -- internal setter functions ------------------- local function setTabbar(tbbr) tabbar = tbbr end ------------------ -- internal getter functions ------------------- local function getterTabbar() return tabbar end ------------------------- -- set external functions ------------------------- globalSpace.setTabbar = setTabbar globalSpace.getTabbar = getterTabbar -- return the table return globalSpace

In my main.lua, I do
globalSpace.setTabbar(tabBar)

and in my scene, I do

local tabbar = globalSpace.getTabbar()
tabbar:setSelected(3)

it works, but is it also a sensible solution?

Hi @christoph-graf,

Yes, if you make a “global storage” as outlined in the Goodbye Globals! tutorial, then your method is perfectly valid.

Using Composer, you could also use the “setVariable()” and “getVariable()” functions to store a reference to the tab bar and access it from other scenes/overlays:

composer.setVariable()

composer.getVariable()

Take care,

Brent

Thank you Brent Sorrentino for going through my code. Appreciate your help and yes, the “Goodbye Globals” tutorial helped a lot. Cool, so Ill leave it the way it is, although the setVariable() and getVariable() sounds cool, too.

Cheers