Tab Bar - Button within a scene to change to a new scene

Hi guys,

I’m trying to find a way of getting my scene to change when using a ui.lua button. This is within the Tab Bar source code on the code exchange.

My tab bar is all setup and working fine. Once I land on scene3.lua I want to be able to press a button within that scene to change to a new scene5.lua which is not a part of the tab bar menu options at the top.

As an alternative, I could skip this entirely and work with Director, but I’m still only part of the way to figuring out how to keep my Tab bar always on top in director and how to make Highlighted buttons.

I’m still fairly new so trying to piece together a working framework is proving difficult.

Before i post some long bits of code on the director version, can anyone make a suggestion on which path might be easier in the end. If I can get the Tab Bar working to change scenes via a button within a scenes that would be all I require in the long run.

Here is what I am trying to do with the scene change button within scene3.lua, I’m not sure what to put in my buttons onPress and/or onRelease function

I’m hoping someone who has used Tab Bar might be able to guide me.

Something a little weird I noticed as well.
When I activate the Press and Release function the button seems to stick in it’s “over” position

Thank you in advanced

[lua]local button1Press = function( event )
if event.phase == “ended” then
loadScreen(“screen2”)
end
end

local button1Release = function( event )
end
local button1 = ui.newButton{
default = “buttonArrow.png”,
over = “buttonArrowOver.png”,
onPress = button1Press,
onRelease = button1Release,
– text = “Next”,
– textColor = { 0, 0, 0, 0 },
id = “next”,
size = 35,
align = “center”,
emboss = true,
}
button1.x = buttonCPPOAS.x + 675
button1.y = buttonCPPOAS.y [/lua]
g:insert(button1) [import]uid: 50045 topic_id: 9462 reply_id: 309462[/import]

I haven’t used Tab Bar myself as I’ve always relied on Director for that kind of thing.

Whichever is easier depends on your own feelings and confidence levels relating to each option.

I would say, though, that if Tab Bar is not something you yet fully understand that Director might be the way to go as it’s fairly simplistic :slight_smile:

Just my two cents.

Good luck to you!
Peach [import]uid: 52491 topic_id: 9462 reply_id: 34657[/import]

Hi Peach,

I remade the whole app in director and it seems to be working minus the highlighted buttons, I keep running into the same problem where my buttons inside the body of the scene1.lua and scene2.lua don’t seem to be working properly.

I’ll have to keep throwing the book at it.

Thanks for the suggestion.

Cheers [import]uid: 50045 topic_id: 9462 reply_id: 34695[/import]

I keep learning a little more each day.

Hopefully someone can comment on the piece of the puzzle I might be missing

Here is my main.lua
I’ve figured out I can call the loadScreen function from within a button on my Screen2.lua.

I just can’t figure out how to keep the Tab Bar from dissapearing as when I call the loadScreen function within my button my whole screen changes to Scene3.lua for example.

Here is my my main.lua and right after is my Scene2.lua that i need to find out what I have to add to make the scene change to only show up in the tabView Group that is in the main.lua.

Can I do that write my button to target the Scene change in the main.lua so it leaves my tab bar on the screen and changes scenes appropriately.

Th director version is working finicky so I keep jumping back and forth until I can get this, seems to be the last piece of the puzzle for me for my entire project.

Any help would be greatly appreciated.

[lua]display.setStatusBar (display.HiddenStatusBar)
–> Hides the status bar
–import external classes
local director = require(“director”)
local ui = require(“ui”)
local viewController = require(“viewController”)
local scrollView = require(“scrollView”)
local util = require(“util”)
local fps = require(“fps”)

local mainView, tabView, currentScreen, tabBar

local function loadScreen(newScreen)
if currentScreen then
currentScreen:cleanUp()
end
currentScreen = require(newScreen).new()
tabView:insert(currentScreen)

return true
end

local function showScreen(event)
local t = event.target
local phase = event.phase

if phase == “ended” then
if t.id == 1 then
loadScreen(“screen1”)
elseif t.id == 2 then
loadScreen(“screen2”)
elseif t.id == 3 then
loadScreen(“screen3”)
elseif t.id == 4 then
loadScreen(“screen4”)
end
tabBar.selected(t)
end

return true
end

local function init()
–Create a group that contains the entire screen and tab bar
mainView = display.newGroup()
–}}
local performance = fps.PerformanceOutput.new();
performance.group.x, performance.group.y = display.contentWidth * .9, display.contentHeight * .15;
performance.alpha = 0.2
–Create a group that contains the screens beneath the tab bar
tabView = display.newGroup()
mainView:insert(tabView)

loadScreen(“screen1”)

tabBar = viewController.newTabBar{
background = “tabBar.png”, --tab bar background
tabs = {“News”, “Tools”, “Resources”, “Products”}, --names to appear under each tab icon
onRelease = showScreen --function to execute when pressed
}
mainView:insert(tabBar)

tabBar.selected()

return true
end

–Start the program!
init()[/lua]

Scene2.lua

[lua]module(…, package.seeall)

function new()
local g = display.newGroup()

local background = display.newRect(0,display.screenOriginY, display.contentWidth, display.contentHeight-display.screenOriginY)
background:setFillColor(255, 255, 255)
g:insert(background)

local message = display.newText(“Screen 2”, 0, 0, native.systemFontBold, 16)
message:setTextColor(0, 0, 0)
message.x = display.contentWidth*0.5
message.y = display.contentHeight*0.5
g:insert(message)

function loadScreen(newScreen)
– if currentScreen then
– currentScreen:cleanUp()
– end
currentScreen = require(newScreen).new()
– tabView:insert(currentScreen)

return true
end

local arrowPress = function( event )

end

local OnRelease = function( event )
if event.phase == “ended” then
loadScreen(“screen1”)
end
end

local buttonArrow = ui.newButton{
default = “buttonArrow.png”,
over = “buttonArrowOver.png”,
onPress = arrowPress,
onRelease = OnRelease,
text = “arrow”,
textColor = { 255, 255, 255, 0 },
id = “arrow”,
size = 30,
align = “center”,
emboss = true,
}
g:insert(buttonArrow)
buttonArrow.x = display.contentWidth * .50
buttonArrow.y = display.contentHeight * .5

function g:cleanUp()
g:removeSelf()
end

return g
end[/lua]
[import]uid: 50045 topic_id: 9462 reply_id: 34696[/import]

I think I will figure this one out. Cheers [import]uid: 50045 topic_id: 9462 reply_id: 34707[/import]

Please tell us how you did it if you solved it. I am trying to create a different set of tabs if a certain value is present in a textfile or not.

But I can’t figure out a way to reload the tabs anyway, any ideas from you? [import]uid: 22737 topic_id: 9462 reply_id: 37797[/import]

Hi Akviby,
I actually ended up creating an illusion more or less.

What I needed was a tab bar with 4 different tabs. The big thing I wanted was that the tab be pressed when you are on that screen and also the tab bar to always be on top of other objects.

I accomplished this by using director.lua.

Basically each of the 4 main screens creates the tab bar each time you load that screen. So essentially you would think there is a tab bar, but in fact each lua screen has the same buttons only slightly edited for each tab, this gives the illusion of the tab bar being one unit when it’s essentially just the same thing 4 different times and slightly modified each time.

The button I want to highlight was accomplished by disactivating the button link and flipping the default button state and the over button state.

I can explain more in detail and show you some code but really mine is an illusion and won’t look that good with director transitions(the special effects, like fade or move from right) if your going from one of the 4 tabs to another. This is because if you move from right, the tab bar washes away on the original screen and comes in again with the new screen.

I think what you want to do could potentially be the same thing as me, and then you add a piece of code that simply hides or shows a group depending on what is saved in the text file.

So you could have 3 buttons that is one group and 3 buttons that is another group and your function hides/shows the correct group dependant on your saved/loaded data to the text file.
I couldn’t get the tab bar example to work properly with director so I ended up going with my low tech solution and making compromises on the original 4 screens to not have any fancy transition effects. Which turned our really well for me anyways.

Let me know if you would like sample code to see what I did if you think this might work for you.
[import]uid: 50045 topic_id: 9462 reply_id: 37994[/import]

Hi Shawnmacewen,

Thanks for sharing with us your work in progress, I’m really new to Corona and I’m looking for somehing very similar to what you’re doing. I’ll appreciate if you post some sample code of your approach.

Thanks in advance,
Emilio
[import]uid: 57355 topic_id: 9462 reply_id: 38177[/import]

Hey
I wanted to be able to control the access to tabs in my app and ended up with the code below. Thought I should share it with others so here it is. I don’t need to mention that I user the viewController.lua but now it’s said :slight_smile:

[lua]local mainView , tabView, currentScreen, tabBar
userID = 0

function loadScreen(newScreen)
if currentScreen then
currentScreen:cleanUp()
end
currentScreen = require(newScreen).new()
tabView:insert(currentScreen)

return true
end

function showScreen(event)
local t = event.target
local phase = event.phase

if phase == “ended” then
if t.id == 1 then
loadScreen(“screen1”)
tabBar.selected(t)
elseif t.id == 2 then
loadScreen(“screen2”)
tabBar.selected(t)
elseif t.id == 3 then
if (tenfingers.isLoggedIN() == true) then
loadScreen(“screen3”)
tabBar.selected(t)
else
native.showAlert(“You have to be logged in to use this tab.”,{“Ok”}, buttonComplete)
end
elseif t.id == 4 then
if (tenfingers.isLoggedIN() == true) then
loadScreen(“screen4”)
tabBar.selected(t)
else
native.showAlert(“You have to be logged in to use this tab.”,{“Ok”}, buttonComplete)
end
end

end

return true
end

function buttonComplete( event )
– do nothing
end[/lua]

As you can see I have created a function called isLoggedIN and that function checks with an SQL SERVER database if the deviceID using the app is a registred user and returns True or False. The buttonComplete function do nothing but needs to be there otherwise the app on the iOS device will crash so you have to have an onComplete routine when OK is clicked.

Hope someone has use for this. [import]uid: 22737 topic_id: 9462 reply_id: 38389[/import]

I ended up creating a mock up Starter application for the iPad when working on this. You can find the source code over at gitHub here.

https://github.com/shawnmacewen/Starter-App

It’s open source and free to use, you might find it handy.

It shows how I created my tab bar and sub menus and how I am working out getting scrollView to work properly for now.

I made it with beginners in mind. It’s meant to be an example anyone can use and people are free to create starter apps upon if they like, the image assets are all free to use.

I started documenting the lua files to give new users an idea of what I was doing, so far the screenIntro.lua and screen1.lua files contain most of the documentation.

You could open this app in the Corona Terminal and it will help you a little with instructions on where to find the lua documentation and I hope to expand upon it at some point. I’ve also tried to include the odd tip on certain pages.

By no means am I an expert and the methods I used are certainly not the best but for new users out there I am sticking to basic functions and concepts so hopefully someone finds it useful.

If anyone finds it useful, has suggestions/feedback or any questions, please let me know. [import]uid: 50045 topic_id: 9462 reply_id: 38497[/import]

@shawnmacewen,
a lot of devs kind of miss what director class is all about or how it works.
If you want to have a TabBar that is common to both the scenes, then that TabBar goes outside of the scenes.

Think of each scene as a card and at one time one card stack is in memory displayed on screen. So if the TabBar is on one of these cards, it will disappear when that card is removed. So the TabBar has to be separate from these cards.

Hope you get the idea, I was trying to make an ASCII text diagram, but then got sidetracked, so maybe later.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 9462 reply_id: 38516[/import]

You are right, however I have never coded before Corona SDK and I spent hours trying to get the Tab Bar example to work with director with no luck. I could get 80-90% of the things working and would run into a major road block each time, Hence the origin of this post.

The problem I had with the tab bar example was I could never get buttons working outside of the tab bar itself to change scene’s or if I could get them working then the Tab Bar would be my problem. I couldn’t have my cake and eat it too with the tab Bar.

I fully admit what I am doing is not the best method, but it works and it is the only way I was able to get what I needed working.

I’m sure as I get better at coding I will make the Tab Bar modular and working in my projects down the road, but for now for us beginners this is pretty easy as it’s only ui buttons and still gives you the tab bar effect.
[import]uid: 50045 topic_id: 9462 reply_id: 38535[/import]

Thanks a lot for your help and time Shawnmacewen! [import]uid: 57355 topic_id: 9462 reply_id: 38752[/import]