IOSはluaからクラスを動的に生成する

2167 ワード

ある时、私たちはコードの过程の中で、私たちが何を书くのか分かりません(淡魂、何を书くのか分かりません).この類は何をするのか分かりませんが、
まずこのクラスを生成することができます
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("
MyView init.
-------") 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("
addALableWithFrame_title.
-------") 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