tabbar view change label

Hi Guys,

How can i change a label for one of the tabbar view buttons

i have a basket button, and i want to change the label of the basket button to show number of items selected so far

i thought it should be something close to:

tabBar.buttons(3):setLabel…

or tabBar:setLabel…

Regards,

Tariq

According to the documentation: https://docs.coronalabs.com/api/library/widget/newTabBar.html, there isn’t a method for doing this.

However, if you read the source, https://github.com/coronalabs/framework-widget/blob/master/widgetLibrary/widget_tabbar.lua, you can find out how the tab bar is made and how you can manually change the texts.

The code is copied from the documentation sample. I only added the last line.

local widget = require( "widget" ) -- Function to handle button events local function handleTabBarEvent( event ) print( event.target.id ) -- Reference to button's 'id' parameter end -- Configure the tab buttons to appear within the bar local tabButtons = { { label = "Tab1", id = "tab1", selected = true, onPress = handleTabBarEvent }, { label = "Tab2", id = "tab2", onPress = handleTabBarEvent }, { label = "Tab3", id = "tab3", onPress = handleTabBarEvent } } -- Create the widget local tabBar = widget.newTabBar( { top = display.contentHeight-120, width = display.contentWidth, buttons = tabButtons } ) tabBar.\_viewButtons[1].label.text = "I've been changed"

Please keep in mind, when we prefix a variable with an underscore, it’s our intent that it be treated as a private variable (since Lua doesn’t support private variables).  We do not intend for you to access those table members and reserve the right to change them at any time. 

Now that said, we’ve not touched that widget in ions. We are not likely going to be adding new features to it anytime soon. We also open-sourced the code for it, so you are free to modify the widget library to your needs (since it’s all just Lua). And if you make something useful like adding an API to update a tab label, we would consider the pull request.

Rob

@XeduR

this worked for me thanks

tabBar._viewButtons[1].label.text = “I’ve been changed”

@Rob

i’m not that good  :D  but it is great to know those facts

According to the documentation: https://docs.coronalabs.com/api/library/widget/newTabBar.html, there isn’t a method for doing this.

However, if you read the source, https://github.com/coronalabs/framework-widget/blob/master/widgetLibrary/widget_tabbar.lua, you can find out how the tab bar is made and how you can manually change the texts.

The code is copied from the documentation sample. I only added the last line.

local widget = require( "widget" ) -- Function to handle button events local function handleTabBarEvent( event ) print( event.target.id ) -- Reference to button's 'id' parameter end -- Configure the tab buttons to appear within the bar local tabButtons = { { label = "Tab1", id = "tab1", selected = true, onPress = handleTabBarEvent }, { label = "Tab2", id = "tab2", onPress = handleTabBarEvent }, { label = "Tab3", id = "tab3", onPress = handleTabBarEvent } } -- Create the widget local tabBar = widget.newTabBar( { top = display.contentHeight-120, width = display.contentWidth, buttons = tabButtons } ) tabBar.\_viewButtons[1].label.text = "I've been changed"

Please keep in mind, when we prefix a variable with an underscore, it’s our intent that it be treated as a private variable (since Lua doesn’t support private variables).  We do not intend for you to access those table members and reserve the right to change them at any time. 

Now that said, we’ve not touched that widget in ions. We are not likely going to be adding new features to it anytime soon. We also open-sourced the code for it, so you are free to modify the widget library to your needs (since it’s all just Lua). And if you make something useful like adding an API to update a tab label, we would consider the pull request.

Rob

@XeduR

this worked for me thanks

tabBar._viewButtons[1].label.text = “I’ve been changed”

@Rob

i’m not that good  :D  but it is great to know those facts