NJCS-方法调用

NJCS内方法的调用有两种方式,这里分别介绍一下:
1:链式调用
代码示例:

var UITableView = getNativeClassWithClassName("UITableView");//根据字符串获取UITableView类
var tableView = UITableView.alloc().init();//初始化UITableView实例
this.view().addSubView(tableView);//获取当前viewController的view 并添加tableView
tableView.setFrame(NJCSCGRect.create(0, 0, NJCSMacors["mainScreenWidth"], NJCSMacors["screenHeight"] - 50));//设置tableView的frame
tableView.setBackgroundColor(NJCSUIColor_JS.create("#999999:0"));//设置tableView的背景颜色
UITableView在执行alloc方法后会返回一个对象,然后在执行该对象的init方法,具体的实现会在[原理文章]()中介绍。 2:簇调用 代码示例:
var cmds = [];
cmds.push(componentCMD_JS.create(component, "icon", "setImage:", [data.icon]));
cmds.push(componentCMD_JS.create(component, "info", "setText:", [data.info]));
cmds.push(componentCMD_JS.create(component, "tip", "setText:", [data.tip]));
cmds.push(componentCMD_JS.create(component, "tip", "setBackgroundColor:", [NJCSUIColor_JS.create("#FFFFFF", 0)]));
cmds.push(componentCMD_JS.create(component, "tip", "setTextColor:", [NJCSUIColor_JS.create("#BBBBBB")]));
var width = NJCSStringUtils.execWidthWithStringFontSizeHeight(data.tip, 11, 13);
cmds.push(componentCMD_JS.create(component, "tip", "setTextAlignment:", ["1"]));
cmds.push(componentCMD_JS.create(component, "tip", "setFrame:", [NJCSCGRect.create(NJCSMacors["screenWidth"] - width - 30, (50 - 15) / 2, width + 6, 15)]));
cmds.push(componentCMD_JS.create(component, "nextIcon", "setImage:", ["job_coin_pusharrow"]));
var infoWidth = NJCSStringUtils.execWidthWithStringFontSizeHeight(data.info, 15, 15);
cmds.push(componentCMD_JS.create(component, "info", "setFrame:", [NJCSCGRect.create(45, 10, infoWidth, 30)]));
cmds.push(componentCMD_JS.create(component, "markView", "setFrame:", [NJCSCGRect.create(45 + infoWidth + 5, 22, 6, 6)]));
if(data.type == "JobQuickPublishOptionViewIdenfitier"){
  cmds.push(componentCMD_JS.create(component, "tip", "setTextColor:", [NJCSUIColor_JS.create("#FF704F")]));
  cmds.push(componentCMD_JS.create(component, "tip", "setBackgroundColor:", [NJCSUIColor_JS.create("#FFEAE5")]));
  cmds.push(componentCMD_JS.create(component, "tip", "setCornerRadius:", [3]));
}
if(data.type == "JobMyCoinViewIdenfitier"){
  var IMPrivateDatabaseManageObject = getNativeClassWithClassName(["IMPrivateDatabaseManageObject"]);
  var imPrivateDBmanager = IMPrivateDatabaseManageObject.alloc().init();
  var clickHot = imPrivateDBmanager.keyValueProxy().stringForKey("JobMyCoinViewIdenfitierHotTip");
  if(clickHot && clickHot == "click"){
      cmds.push(componentCMD_JS.create(component, "markView", "setHidden:", [true]));
    }else{
      cmds.push(componentCMD_JS.create(component, "markView", "setHidden:", [false]));
  }
}
bridgeJS.sendCMDTOComponents(cmds);

2-1:使用componentCMD_JS.create(component, “icon”, “setImage:”, [data.icon]);创建一个cmd
源码如下:

var componentCMD_JS = {
  create:function(component, childInstanceName, methodName, parameters){
    var componentCMD = {};
    componentCMD.component = component;
    componentCMD.childInstanceName = childInstanceName;
    componentCMD.methodName = methodName;
    componentCMD.parameters = parameters;
    return componentCMD;
  }
}

参数介绍:
component:被操作的对象,这个对象既可以是Native返回的亦可以是JS封装后的实例
childInstanceName:component的子组件,如果为“”则被操作对象既为component本身
methodName:方法的名字
parameters:参数
2-2:将生成的cmd push进数组
2-3:通过bridgeJS.sendCMDTOComponents(cmds);将cmd数组发送给Native执行,不管数组内有多少个cmd,Native和JS的交互只有一次
NJCSUITableViewCell即使以这种方式调用设置各种属性的

热评文章