Trouble using storyboard with tabbar

I am trying to build a template for having a tab bar work in conjunction with some onscreen buttons on different scenes. Basically the buttons on the different scenes function exactly like the tab bar and invoke a scene change.

I am able to add my tab bar, and it works great by itself across all the scenes, but I am having trouble calling tabBar:pressButton( ) function from outside of the main.lua file. I am just not sure how to target that properly. I keep getting this error “attempt to index global ‘tabBar’ a nil value”

Any help would be greatly appreciated. [import]uid: 150370 topic_id: 33395 reply_id: 333395[/import]

Can you post some code or code snippets for us to look at? In particular where you setup your tabbar and where you are trying to call the tabbar. Remember to put them inside of <code> and </code> tags.

[import]uid: 199310 topic_id: 33395 reply_id: 132658[/import]

Sure
to sum it up, I am adding the tabbar in main.lua so it will persist, it does everything fine including changing scenes without issue. I am just wanting to be able to call tabBar:pressButton( 1 ) from any lua file that isn’t main.lua and I’m not sure how to do that properly.

main.lua

local storyboard = require "storyboard"  
local widget = require "widget"  
  
 local function goTo( event )  
 if event.target.id==1 then  
 storyboard.gotoScene( "home\_screen" )  
 elseif event.target.id==2 then  
 storyboard.gotoScene( "menu1" )  
 elseif event.target.id==3 then  
 storyboard.gotoScene( "menu2" )  
 elseif event.target.id==3 then  
 storyboard.gotoScene( "documents\_menu" )  
 else  
 print("ERROR - unknown tab ID passed to tabBar")  
 end  
 end  
  
local tabButtons = {  
 { label="Home", up="assets/icons/home\_24.png", down="assets/icons/home\_24\_hi.png", width = 24, height = 24, selected=true, onPress = goTo },  
 { label="cobas b 123", up="assets/icons/menu1\_24.png", down="assets/icons/menu1\_24\_hi.png", width = 24, height = 24, onPress = goTo },  
 { label="cobas b221", up="assets/icons/menu2\_24.png", down="assets/icons/menu2\_24\_hi.png", width = 24, height = 24, onPress = goTo },  
 { label="Documents", up="assets/icons/document\_24.png", down="assets/icons/document\_24\_hi.png", width = 24, height = 24, onPress = goTo },  
}  
 local tabBar = widget.newTabBar{  
 top = display.contentHeight - 50,   
 buttons = tabButtons  
 }  
  
storyboard.gotoScene( "home\_screen" )  

What I am hoping to accomplish is that when I click on an image in home_screen, menu1, or menu2 I can update the state of the tabBar to highlight the correct area the user is currently in.

I have tried calling it from within event listeners placed on buttons and just as the other lua enterScene, both of which are giving me the error I mentioned earlier.

for example

in home_screen.lua

function scene:enterScene( event )  
 tabBar:pressButton( 1 )  
end  

So I feel like what I am doing is missing some fundamental aspect of targeting tabBar correctly.
[import]uid: 150370 topic_id: 33395 reply_id: 132668[/import]

Taught me something new!!! I didn’t know the pressButton method existed.

Where are you creating tabBar in main.lua?

If you’re doing:

local tabBar = widget.newTabBar()

Then tabBar is only going to be known about in main.lua and your other lua modules won’t have a clue it exists, giving you the error you see.

If you leave off the “local”, making it global then what you’re trying to do should work. Globals of course are considered evil, so you can do something like:

storyboard.myTabBar = tabBar  

Then when you need it in a storyboard based lua file, you can do

storyboard.tabBar:pressButton( 1 )  

All we are doing is adding it to the storyboard object (which is a big table) [import]uid: 199310 topic_id: 33395 reply_id: 132697[/import]

That makes perfect sense, thanks for your help.
[import]uid: 150370 topic_id: 33395 reply_id: 132754[/import]

Can you post some code or code snippets for us to look at? In particular where you setup your tabbar and where you are trying to call the tabbar. Remember to put them inside of <code> and </code> tags.

[import]uid: 199310 topic_id: 33395 reply_id: 132658[/import]

Sure
to sum it up, I am adding the tabbar in main.lua so it will persist, it does everything fine including changing scenes without issue. I am just wanting to be able to call tabBar:pressButton( 1 ) from any lua file that isn’t main.lua and I’m not sure how to do that properly.

main.lua

local storyboard = require "storyboard"  
local widget = require "widget"  
  
 local function goTo( event )  
 if event.target.id==1 then  
 storyboard.gotoScene( "home\_screen" )  
 elseif event.target.id==2 then  
 storyboard.gotoScene( "menu1" )  
 elseif event.target.id==3 then  
 storyboard.gotoScene( "menu2" )  
 elseif event.target.id==3 then  
 storyboard.gotoScene( "documents\_menu" )  
 else  
 print("ERROR - unknown tab ID passed to tabBar")  
 end  
 end  
  
local tabButtons = {  
 { label="Home", up="assets/icons/home\_24.png", down="assets/icons/home\_24\_hi.png", width = 24, height = 24, selected=true, onPress = goTo },  
 { label="cobas b 123", up="assets/icons/menu1\_24.png", down="assets/icons/menu1\_24\_hi.png", width = 24, height = 24, onPress = goTo },  
 { label="cobas b221", up="assets/icons/menu2\_24.png", down="assets/icons/menu2\_24\_hi.png", width = 24, height = 24, onPress = goTo },  
 { label="Documents", up="assets/icons/document\_24.png", down="assets/icons/document\_24\_hi.png", width = 24, height = 24, onPress = goTo },  
}  
 local tabBar = widget.newTabBar{  
 top = display.contentHeight - 50,   
 buttons = tabButtons  
 }  
  
storyboard.gotoScene( "home\_screen" )  

What I am hoping to accomplish is that when I click on an image in home_screen, menu1, or menu2 I can update the state of the tabBar to highlight the correct area the user is currently in.

I have tried calling it from within event listeners placed on buttons and just as the other lua enterScene, both of which are giving me the error I mentioned earlier.

for example

in home_screen.lua

function scene:enterScene( event )  
 tabBar:pressButton( 1 )  
end  

So I feel like what I am doing is missing some fundamental aspect of targeting tabBar correctly.
[import]uid: 150370 topic_id: 33395 reply_id: 132668[/import]

Taught me something new!!! I didn’t know the pressButton method existed.

Where are you creating tabBar in main.lua?

If you’re doing:

local tabBar = widget.newTabBar()

Then tabBar is only going to be known about in main.lua and your other lua modules won’t have a clue it exists, giving you the error you see.

If you leave off the “local”, making it global then what you’re trying to do should work. Globals of course are considered evil, so you can do something like:

storyboard.myTabBar = tabBar  

Then when you need it in a storyboard based lua file, you can do

storyboard.tabBar:pressButton( 1 )  

All we are doing is adding it to the storyboard object (which is a big table) [import]uid: 199310 topic_id: 33395 reply_id: 132697[/import]

That makes perfect sense, thanks for your help.
[import]uid: 150370 topic_id: 33395 reply_id: 132754[/import]

Thanks but what if “myTabBar” is in “main.lua”, I have a  “mainscreen.lua” where I cam calling “myTabBar:setSelected(3, true)” but it reports that “myTabBar” is nil.

 

I removed the local in main.lua and tried "_G.myTabBar etc but it still cant set the selected tab.  myTabBar is global (by removing local in main.lua

 

I have added “storyboard.myTabBar = tabBar” to main.lua but the tabbar is not accessible from other screens.

http://forums.coronalabs.com/topic/27872-trouble-using-storyboard-with-tabbar/?hl=%2Bmytabbar%3Asetselected%281%2C+%2Btrue%29#entry149845

 

I have removed local to make it global too.

 

Stumped

 

Fixed: I was trying to call myTabBar.

 

Tip you can deselect everything by typing: tabBar:setSelected(0, false)

Thanks but what if “myTabBar” is in “main.lua”, I have a  “mainscreen.lua” where I cam calling “myTabBar:setSelected(3, true)” but it reports that “myTabBar” is nil.

 

I removed the local in main.lua and tried "_G.myTabBar etc but it still cant set the selected tab.  myTabBar is global (by removing local in main.lua

 

I have added “storyboard.myTabBar = tabBar” to main.lua but the tabbar is not accessible from other screens.

http://forums.coronalabs.com/topic/27872-trouble-using-storyboard-with-tabbar/?hl=%2Bmytabbar%3Asetselected%281%2C+%2Btrue%29#entry149845

 

I have removed local to make it global too.

 

Stumped

 

Fixed: I was trying to call myTabBar.

 

Tip you can deselect everything by typing: tabBar:setSelected(0, false)

Thank you. This solved an issue for me.

The takeaway was:

in Main:


local localTabs = widget.newTabBar{}

storyboard.tabBar = localTabs


When linking from a storyboard lua that falls under one tab to a storyboard that falls under another tab, my equivalent of a goto statement is:


    storyboard.tabBar:setSelected(2, true) --WHERE 2 IS THE TAB TO SELECT, AND TRUE SIMULATES THE PRESS


Thank you. This solved an issue for me.

The takeaway was:

in Main:


local localTabs = widget.newTabBar{}

storyboard.tabBar = localTabs


When linking from a storyboard lua that falls under one tab to a storyboard that falls under another tab, my equivalent of a goto statement is:


    storyboard.tabBar:setSelected(2, true) --WHERE 2 IS THE TAB TO SELECT, AND TRUE SIMULATES THE PRESS