How to show a tabBar in certain scenes and not the others?

I have a tabBar with 3 tabs to access 3 different scenes. In order to have the tabBar appear and function in all 3 scenes, I did not add it to the sceneGroup in its original creation scene. (is this even the right way?)

Now,  each of those scenes has buttons to access different scenes that should have no access to the tabBar. So, how would I go about not showing the tabBar in those scenes?

Just store a global reference to the tabBar, and then you can access it in any scene.

There’s a few ways of doing this.

  1. Store as a literal global:

[lua]

_G.tabBar = myTabBar

_G.tabBar.alpha = 0  – to hide

[/lua]

  1. Create a globals module that is required in every scene:

[lua]

– globals.lua –

local m = {}

  m.someValue = 5000;

return m

– sceneWhereTabBarCreated.lua –

local glo = require("globals.lua)

glo.tabBar = myTabBar  – create tab bar and assign to glo

print(glo.someValue)  – prints 5000

glo.someValue = glo.someValue + 500

glo.someOtherValueIMightWant = 45

– anotherSceneToAccessTabBar.lua –

local glo = require("globals.lua)

glo.tabBar.alpha = 0  – hide the tabBar

print (glo.someValue) – will print 5500

print (glo.someOtherValueIMightWant)   – will print 45

[/lua]

Method 1

Keep your controls independent of the scenes and place them in their own group.  

Then, add an enterFrame listener to the group to bring it to the front every frame.

Add additional code to hide/show the bar as needed.

Put all this code in a module and call that module from your scenes.

Method 2

Same, but don’t use composer.  Use your own scene management code.

Need More Help?  Hire A Hitman. - Just guessing, but a sample showing how to do method #1 would be a level 1 Hit.

Thanks, guys, I will definitely try adding the tabBar in its own module and call it from there.

I always avoid anything that even approaches the global space (because there are countless ways to avoid it, for example here). So, for this scenario, I would suggest you put the TabBar on the Composer “stage” which is like the parent display group that contains all other Composer scenes. That means it’s “common” and will take preference over individual scenes.

It’s documented here:

https://docs.coronalabs.com/api/library/composer/stage.html

Brent

Learned something new.  Although it’s right in the docs, I didn’t realize there was a stage.  Great suggestion Brent.

Just store a global reference to the tabBar, and then you can access it in any scene.

There’s a few ways of doing this.

  1. Store as a literal global:

[lua]

_G.tabBar = myTabBar

_G.tabBar.alpha = 0  – to hide

[/lua]

  1. Create a globals module that is required in every scene:

[lua]

– globals.lua –

local m = {}

  m.someValue = 5000;

return m

– sceneWhereTabBarCreated.lua –

local glo = require("globals.lua)

glo.tabBar = myTabBar  – create tab bar and assign to glo

print(glo.someValue)  – prints 5000

glo.someValue = glo.someValue + 500

glo.someOtherValueIMightWant = 45

– anotherSceneToAccessTabBar.lua –

local glo = require("globals.lua)

glo.tabBar.alpha = 0  – hide the tabBar

print (glo.someValue) – will print 5500

print (glo.someOtherValueIMightWant)   – will print 45

[/lua]

Method 1

Keep your controls independent of the scenes and place them in their own group.  

Then, add an enterFrame listener to the group to bring it to the front every frame.

Add additional code to hide/show the bar as needed.

Put all this code in a module and call that module from your scenes.

Method 2

Same, but don’t use composer.  Use your own scene management code.

Need More Help?  Hire A Hitman. - Just guessing, but a sample showing how to do method #1 would be a level 1 Hit.

Thanks, guys, I will definitely try adding the tabBar in its own module and call it from there.

I always avoid anything that even approaches the global space (because there are countless ways to avoid it, for example here). So, for this scenario, I would suggest you put the TabBar on the Composer “stage” which is like the parent display group that contains all other Composer scenes. That means it’s “common” and will take preference over individual scenes.

It’s documented here:

https://docs.coronalabs.com/api/library/composer/stage.html

Brent

Learned something new.  Although it’s right in the docs, I didn’t realize there was a stage.  Great suggestion Brent.