Skip to content
probablycorey edited this page May 16, 2011 · 7 revisions

Install Wax

We recommend that you use Xcode 3 for Wax projects (for now). Xcode 4's template system is still a little wonky so just use Xcode 3.

  1. Download wax or use git and clone it http://github.com/probablycorey/wax/

  2. From the shell, cd into wax folder created above and type rake install. This will install an xcode project template.

  3. Don't know Lua? If you are familiar with Javascript, Python, Ruby or any dynamic language you can learn the basics of Lua in 15 minutes! You can check out these free tutorials or buy a copy of Programing in Lua.

Create a new project

  1. Open up xcode and create a new Wax project, it should be located under the User Tempates section.

  2. From now on when we write APP_ROOT we are referencing the path of this Xcode project.

  3. Run the app! You'll get a simple "Hello Lua" program running in your iPhone Simulator!

Let's write some Lua!

  • You don't need to edit the Lua code from Xcode, it's all located in the APP_ROOT/scripts directory, so open up your favorite text editor and go at it! You should also look at Wax's Standard Library files at APP_ROOT/wax/lib/stdlib. These contain helpful code you can use!

    • If you want to use TextMate type rake tm from APP_ROOT to setup a project. Also install the wax-bundle for some helpful keyboard shortcuts.
    • If Xcode strikes your fancy, you could try capgo.com's syntax highlighting plugin.
    • If you use vi or Emacs, you probably know how to install Lua support by yourself.
  • To create the TableView, first create a new file called APP_ROOT/scripts/MyTableViewController.lua

  • Create a new Objective-C controller class with this call

    waxClass{"MyTableViewController", UITableViewController}
  • Now add the init function, it will setup the tableView's contents and call the super's init (just like in Objective-C!)
    function init(self)
      self.super:initWithStyle(UITableViewStylePlain)

      -- Here are the tableView's contents
      self.things = {"Planes", "Trains", "Automobiles"}

      return self
    end
  • Now we need to implement methods from the UIDataSource protocol. The final MyTableViewController.lua file should look like this.
    waxClass{"MyTableViewController", UITableViewController}
        
    function init(self)
      self.super:initWithStyle(UITableViewStylePlain)

       -- Here are the tableView's contents
      self.things = {"Planes", "Trains", "Automobiles"}

      return self
    end
        
    function numberOfSectionsInTableView(self, tableView)
      return 1
    end

    function tableView_numberOfRowsInSection(self, tableView, section)
      return #self.things
    end

    function tableView_cellForRowAtIndexPath(self, tableView, indexPath)
      local identifier = "MyTableViewControllerCell"
      local cell = tableView:dequeueReusableCellWithIdentifier(identifier) or
                   UITableViewCell:initWithStyle_reuseIdentifier(UITableViewCellStyleDefault, identifier)

      local thing = self.things[indexPath:row() + 1] -- Must +1 because Lua arrays are 1 based
      cell:textLabel():setText(thing)

      return cell
    end
  • All that is left is to create an instance of MyTableViewController and add it to the main window. We do this by editing APP_ROOT/scripts/AppDelegate.lua. This is the same as the UIApplicationDelegate you use in an Objective-C app.
    require "MyTableViewController"

    waxClass{"AppDelegate", protocols = {"UIApplicationDelegate"}}

    function applicationDidFinishLaunching(self, application)
      local frame = UIScreen:mainScreen():bounds()
      self.window = UIWindow:initWithFrame(frame)
       
      self.controller = MyTableViewController:init()
      self.window:addSubview(self.controller:view())
       
      self.window:makeKeyAndVisible()
    end
  • Run the app... You will have a really simple UITableView that was created via Lua! It's up to you to make it awesome.
Clone this wiki locally