How can I manually change the selected tabButton of a widget.newTabBar

Hello

How can I manually change the selected tabButton of a widget.newTabBar

I have a main.lua with a tab bar with 6 buttons, i can load each sub screen by clicking the tabs but if I have a button that loads a scene (storyboard.gotoScene( “menuscreen”, “fromRight”, 400)) the active tab does not change.

Is there a global function that I can call to change the selected tab button.

I also have 6 mains screens but each screen has further sub screens, I want to keep the main tab bar buttons but change the selected tab depending on the screen loaded.

Thanks

My tabbar object is called “myTabBar”. In order to force a button press on it, I do this:

myTabBar:setSelected(3, true)

The first argument is the tab number you want to “press”. The second argument is a bool stating whether or not you want the tab bar to actually change the image of the currently selected tab.

E.g If I was on page 2, and click a button that takes me to page 3, the above function will go to page 3 and light up the 3rd tab bar button. If it was set to false, I would still go to page 3, but the 2nd tab bar button would remain lit.

Here’s the api doc for this function: http://docs.coronalabs.com/api/type/TabBarWidget/setSelected.html

I’ll include some of the creation code, just so you can see how I’ve done it. 

Hopefully your setup is fairly similar:

onTabPress = function( event ) print("pressed tab "..event.target.id ) storyboard.goToScene("page"..event.target.id) end local tabButtons = { { font = myFont, label="Tab 1", defaultFile="images/tab\_1.png", overFile="images/tab\_1\_down.png", width= 100, height= 50, size = 20, onPress=onTabPress, selected=true }, { font = myFont, label="Tab 2", defaultFile="images/tab\_2.png", overFile="images/tab\_2\_down.png", width= 100, height= 50, size = 20, onPress=onTabPress, }, { font = myFont, label="Tab 3", defaultFile="images/tab\_3.png", overFile="images/tab\_3\_down.png", width= 100, height= 50, size = 20, onPress=onTabPress, }, { font = myFont, label="Tab 4", defaultFile="images/tab\_4.png", overFile="images/tab\_4\_down.png", width= 100, height= 50, size = 20, onPress=onTabPress, }, } --create tab bar object using buttons defined above myTabBar = widget.newTabBar{ width = display.contentWidth, height = 100, left = 0, top = display.contentHeight-100, buttons =tabButtons }

Thanks but what if “myTabBar” is in “main.lua”, I ahvea  “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.

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)

My tabbar object is called “myTabBar”. In order to force a button press on it, I do this:

myTabBar:setSelected(3, true)

The first argument is the tab number you want to “press”. The second argument is a bool stating whether or not you want the tab bar to actually change the image of the currently selected tab.

E.g If I was on page 2, and click a button that takes me to page 3, the above function will go to page 3 and light up the 3rd tab bar button. If it was set to false, I would still go to page 3, but the 2nd tab bar button would remain lit.

Here’s the api doc for this function: http://docs.coronalabs.com/api/type/TabBarWidget/setSelected.html

I’ll include some of the creation code, just so you can see how I’ve done it. 

Hopefully your setup is fairly similar:

onTabPress = function( event ) print("pressed tab "..event.target.id ) storyboard.goToScene("page"..event.target.id) end local tabButtons = { { font = myFont, label="Tab 1", defaultFile="images/tab\_1.png", overFile="images/tab\_1\_down.png", width= 100, height= 50, size = 20, onPress=onTabPress, selected=true }, { font = myFont, label="Tab 2", defaultFile="images/tab\_2.png", overFile="images/tab\_2\_down.png", width= 100, height= 50, size = 20, onPress=onTabPress, }, { font = myFont, label="Tab 3", defaultFile="images/tab\_3.png", overFile="images/tab\_3\_down.png", width= 100, height= 50, size = 20, onPress=onTabPress, }, { font = myFont, label="Tab 4", defaultFile="images/tab\_4.png", overFile="images/tab\_4\_down.png", width= 100, height= 50, size = 20, onPress=onTabPress, }, } --create tab bar object using buttons defined above myTabBar = widget.newTabBar{ width = display.contentWidth, height = 100, left = 0, top = display.contentHeight-100, buttons =tabButtons }

Thanks but what if “myTabBar” is in “main.lua”, I ahvea  “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.

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)