美文网首页
Wax框架简明教程(2)安装

Wax框架简明教程(2)安装

作者: 祥子_HelloWorld | 来源:发表于2019-08-03 00:53 被阅读0次
一、安装Wax

当前,我们推荐你使用Xcode3创建Wax项目。Xcode4的项目模板仍然有些问题,因此请使用Xcode3。

  1. http://github.com/probablycorey/wax/下载Wax或者用git命令将它复制到本地
  2. 在shell中,转到wax文件夹,输入rake install命令。这将安装xcode项目模板。
  3. 你知道Lua吗?如果你熟悉Javascript、Python、Runby之类的动态语言,你通过5分钟的学习就能入门。你可以阅读这些免费教程或者买本《Programingin Lua》
二、创建新项目
  1. 打开Xcode,创建一个 Wax项目,该模板可以在 User Templates 中找到。
  2. 从现在开始,APP_ROOT将代表项目本身所在的路径。
  3. 运行app,你将在iPhone模拟器中看到Hello Lua字样。
三、开始使用Lua
  • 并不需要在Xcode中编写Lua代码,它们全都在APP_ROOT/scripts目录下,直接用你喜欢的文本编辑工具编辑它们好了。Wax标准库文件位于APP_ROOT/wax/lib/stdlib目录下。这些全是你会用到的代码!
    如果你使用TextMate,从APP_ROOT目录下执行rake tm,以便从项目打开。还可以安装wax-bundle已支持快捷键。
    如果你迷恋于Xcode,你可以试试 capgo.com's 的语法加亮插件。
    如果你使用的是vi或Emacs,你总不会连Lua支持都不会装吧?

  • 要创建一个TableView,首先创建一个文件: APP_ROOT/scripts/MyTableViewController.lua

  • 要创建新的OC控制器类,使用:
    waxClass{"MyTableViewController",UITableViewController}

  • 现在实现init函数,在里面设置tableView的内容并调用父类的init方法(正如你在OC中所做的一样)。

function init(self)
   self.super:initWithStyle(UITableViewStylePlain)
    -- Here are the tableView'scontents
    self.things ={"Planes", "Trains", "Automobiles"}
    return self
  end
  • 接下来实现UIDataSource协议方法。最终 MyTableViewController.lua 文件如下所示:
waxClass{"MyTableViewController",UITableViewController}

  function init(self)
   self.super:initWithStyle(UITableViewStylePlain)
     -- Here are thetableView's contents
    self.things ={"Planes", "Trains", "Automobiles"}
    return self
  end

  functionnumberOfSectionsInTableView(self, tableView)
    return 1
  end

  functiontableView_numberOfRowsInSection(self, tableView, section)
    return #self.things
  end

  functiontableView_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
  • 最后就是创建MyTableViewController示例并加到主窗口中。这需要修改APP_ROOT/scripts/AppDelegate.lua。它跟我们在OC程序中的UIApplicationDelegate是一样的。
require"MyTableViewController"

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

  functionapplicationDidFinishLaunching(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
  • 运行程序,你已经用Lua创建了一个真正的UITableView。
四、Wax framework安装

在这里下载framework:https://github.com/downloads/probablycorey/wax/wax.framework.zip

五、在项目中以framwork方式使用wax

  1. 用Xcode打开项目,将wax.framework拖到Xcode的frameworks组下。确保勾选"Copy items into destination group's folder"。

  2. 新建init.lua(确保加到了应用程序束中)。在文件中加入代码:

puts("ZOMG, LUA IS RUNNING")  
puts("Here is Lua talking to ObjC%s", tostring(UIApplication:sharedApplication()))
  1. 打开AppDelegate文件,导入wax头文件:
#import "wax/wax.h" 
  1. 在AppDelegate的application:didFinishLaunchingWithOptions:方法中加入:
wax_start("init.lua",nil);
 // To add wax with extensions, use thisline instead
 // #import "wax/wax_http.h"
 // #import "wax/wax_json.h"
 // #import"wax/wax_filesystem.h"
 // wax_start("init.lua",luaopen_wax_http, luaopen_wax_json, luaopen_wax_filesystem, nil);

最后运行,你将在Xcode控制台重看到Lua输出的内容。

参考:http://blog.csdn.net/kmyhy/article/details/8048083

相关文章

网友评论

      本文标题:Wax框架简明教程(2)安装

      本文链接:https://www.haomeiwen.com/subject/tiakdctx.html