Tab Bar

Features demonstrated:

How to create a tab bar that allows the user to navigate between screens, using the View Controller Library, viewController.lua.

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
--import external classes
local ui = require("ui")
local viewController = require("viewController")
 
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")
                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()   
 
        --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 = {"Play", "News", "About"}, --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()