现在的位置: 首页 > 综合 > 正文

IOS从lua动态生成类

2018年05月25日 ⁄ 综合 ⁄ 共 1573字 ⁄ 字号 评论关闭

有的时候我们在编码的过程中,不知道我们要写什么类(淡魂,不知道还写啥?),不知道这个类要干什么,

我们可以先把生成这个类写好,

oc里初始化一个MyView对象

wax_start("init.lua", nil);
    
    id tmpView = [[NSClassFromString(@"MyView") alloc] init];
    [self.view addSubview:tmpView];
    [tmpView release];

具体的执行代码放到lua里写,可以放到本地,也可以从网上动态下载.

MyView 继承UIView,

有一个addALable的方法, 下面的是lua

waxClass{"MyView", UIView}
function init(self)
  self.super:init()
  self:setFrame(CGRect(0,0,200.0,200.0))
  self:setBackgroundColor(UIColor:redColor())
  print("\nMyView init.\n-------")
  return self
end

function addALable(self)
  local label = UILabel:initWithFrame(CGRect(0, 20, 100, 40))
  label:setFont(UIFont:boldSystemFontOfSize(16))
  label:setColor(UIColor:whiteColor())
  label:setBackgroundColor(UIColor:colorWithRed_green_blue_alpha(0.173, 0.651, 0.627, 1))
  label:setText("----测试页面----")
  label:setTextAlignment(UITextAlignmentCenter)
  self:addSubview(label)
end

调用 tmpView 的 addALabel方法

    if (tmpView && [tmpView respondsToSelector:@selector(addALable)]) {
        objc_msgSend(tmpView, @selector(addALable));
    }

带多个参数的Lua的写法

function addALableWithTitle_color(self, aStr, aColor)
  local label = UILabel:initWithFrame(CGRect(0, 120, 100, 40))
  label:setFont(UIFont:boldSystemFontOfSize(16))
  label:setColor(UIColor:whiteColor())
  label:setBackgroundColor(aColor)
  label:setText(aStr)
  label:setTextAlignment(UITextAlignmentCenter)
  self:addSubview(label)
  print("\naddALableWithFrame_title.\n-------")
end

oc里调用的写法

    if (tmpView && [tmpView respondsToSelector:@selector(addALableWithTitle:color:)]) {
        objc_msgSend(tmpView, @selector(addALableWithTitle:color:), @"test", [UIColor blueColor]);
    }

dome 下载地址

http://download.csdn.net/detail/uxyheaven/5988821

需要用到的

 ios 支持lua 的类

https://github.com/probablycorey/wax

抱歉!评论已关闭.