当前位置: 首页 > news >正文

JSPatch

JSPacth(用JS更新OC代码)

临时修复一些BUG需要这个方案

库:https://github.com/bang590/JSPatch

工具: http://bang590.github.io/JSPatchConvertor/

官网:http://jspatch.com

 

下面是我用js代码写的app的一些类和方法

//defineClass重新定义现有的类并且覆盖方法

//tips:我的使用方式,使用oc-js转换工具转换之后再微调,不然那些类的方法好难记住

//方法和方法之间需要用逗号隔开
require('UIAlertView')//需要使用该类就需要导入
defineClass('MainControl:<UIAlertViewDelegate>', {
    
    //这里覆盖了生命周期的一个方法,让它打印一个信息
    viewWillAppear: function(animated) {
        self.super().viewWillAppear(animated);
        console.log('生命周期-视图将要显示')
    },
    
    //这里覆盖了oc中的showText使其调用为空
    showText: function() {
            
    },
            
    //按钮点击监听事件
    handleBtn: function(sender) {
        console.log('测试1111')
        var testString = "测试的字符串"
        console.log('click btn ' + testString)
        
        //初始化一个alertView
        var temAlertView = UIAlertView.alloc().
        initWithTitle_message_delegate_cancelButtonTitle_otherButtonTitles("提示","点击进入自定义列表控制器", self, "确定", null);
        temAlertView.show()
            
    },
    //alertView的代理
    alertView_willDismissWithButtonIndex: function(alertView, idx) {
        //控制器跳转
        var control = CustomTableControl.alloc().init();
        self.navigationController().pushViewController_animated(control, YES)
    }
})

//我自己创建一个控制器(自定义列表控制器)
require('UITableViewCell')
defineClass('CustomTableControl: UITableViewController', {
            
            //生命周期
            viewDidLoad: function() {
                self.super().viewDidLoad();
                self.setTitle("列表");
                self.tableView().registerClass_forCellReuseIdentifier(UITableViewCell.class(), "cellID");
            },
            //代理
            tableView_numberOfRowsInSection: function(tableView, section) {
                return 10;
            },
            
            tableView_cellForRowAtIndexPath: function(tableView, indexPath) {
                //创建cell
                var cell = tableView.dequeueReusableCellWithIdentifier("cellID");
                cell.textLabel().setText("text");
                return cell;
            },
            tableView_didSelectRowAtIndexPath: function(tableView, indexPath) {
                var control = CustomDeatilControl.alloc().init();
                self.navigationController().pushViewController_animated(control, YES)
            },
})

require('UIColor,UIView');
//自己创建的一个视图控制器
defineClass('CustomDeatilControl: UIViewController', {
            
            //生命周期
            viewDidLoad: function() {
                self.super().viewDidLoad();
                self.setTitle("详情");
                self.view().setBackgroundColor(UIColor.redColor());
            //frame初始化要这种格式 UIView.alloc().initWithFrame({x:20, y:20, width:100, height:100});
                var view = UIView.alloc().initWithFrame({x:100, y:100, width:100, height:100});
                view.setBackgroundColor(UIColor.blackColor());
                self.view().addSubview(view);
            },
})

 

效果图:

 

 

一般情况下我们照着下面方法的使用,已经能处理很多事情了

//require
require('UIView, UIColor, UISlider, NSIndexPath')

// Invoke class method
var redColor = UIColor.redColor();

// Invoke instance method
var view = UIView.alloc().init();
view.setNeedsLayout();

// set proerty
view.setBackgroundColor(redColor);

// get property 
var bgColor = view.backgroundColor();

// multi-params method (use underline to separate)
// OC:NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1];
var indexPath = NSIndexPath.indexPathForRow_inSection(0, 1);

// method name contains underline (use double undeline to represent)
// OC: [JPObject _privateMethod];
JPObject.__privateMethod()

// use .toJS() to convert NSArray / NSString / NSDictionary to JS type.
var arr = require('NSMutableArray').alloc().init()
arr.addObject("JS")
jsArr = arr.toJS()
console.log(jsArr.push("Patch").join(''))  //output: JSPatch

// use hashes to represent struct like CGRect / CGSize / CGPoint / NSRange
var view = UIView.alloc().initWithFrame({x:20, y:20, width:100, height:100});
var x = view.bounds().x;

// wrap function with `block()` when passing block from JS to OC
// OC Method: + (void)request:(void(^)(NSString *content, BOOL success))callback
require('JPObject').request(block("NSString *, BOOL", function(ctn, succ) {
  if (succ) log(ctn)
}));

// GCD
dispatch_after(function(1.0, function(){
  // do something
}))
dispatch_async_main(function(){
  // do something
})

 

推荐使用oc转js代码和自己改进细节方法使用,提高效率

demo链接:http://pan.baidu.com/s/1i4yEejV

 

转载于:https://www.cnblogs.com/hxwj/p/5163158.html

相关文章:

  • RSA加密的测试demo
  • 用 Python 开发自动化测试脚本
  • 运用.net core配合VS 2015制作nuget包
  • linux shell数据重定向(输入重定向与输出重定向)详细分析 上(转)
  • PHP开发学习门户改版效果图投票
  • 收到云栖社区发的淘公仔礼物
  • d语言之模块化
  • 7.12 Models -- Frequently Asked Questions
  • Mysql错误问题记录
  • PostgreSQL wal receiver 统计信息 patch
  • 【COCOS CREATOR 系列教程之四】基于0.7.1先简单制作一个PAGEVIEW
  • 两列自适应布局方案整理
  • 在.net桌面程序中自定义鼠标光标
  • Beanstalkd中文协议解读
  • windows ping RPi 2B
  • (ckeditor+ckfinder用法)Jquery,js获取ckeditor值
  • Android开源项目规范总结
  • Angularjs之国际化
  • avalon2.2的VM生成过程
  • JavaScript服务器推送技术之 WebSocket
  • JS 面试题总结
  • Markdown 语法简单说明
  • MYSQL 的 IF 函数
  • October CMS - 快速入门 9 Images And Galleries
  • Spring Boot快速入门(一):Hello Spring Boot
  • SSH 免密登录
  • 半理解系列--Promise的进化史
  • 创建一个Struts2项目maven 方式
  • 搭建gitbook 和 访问权限认证
  • 工作中总结前端开发流程--vue项目
  • 基于HAProxy的高性能缓存服务器nuster
  • 前端工程化(Gulp、Webpack)-webpack
  • 深度解析利用ES6进行Promise封装总结
  • 算法-插入排序
  • 小程序开发中的那些坑
  • 用 vue 组件自定义 v-model, 实现一个 Tab 组件。
  • 云栖大讲堂Java基础入门(三)- 阿里巴巴Java开发手册介绍
  • 正则与JS中的正则
  • 终端用户监控:真实用户监控还是模拟监控?
  • ​第20课 在Android Native开发中加入新的C++类
  • ​云纳万物 · 数皆有言|2021 七牛云战略发布会启幕,邀您赴约
  • # include “ “ 和 # include < >两者的区别
  • #stm32驱动外设模块总结w5500模块
  • $.type 怎么精确判断对象类型的 --(源码学习2)
  • (+3)1.3敏捷宣言与敏捷过程的特点
  • (1)(1.19) TeraRanger One/EVO测距仪
  • (1)STL算法之遍历容器
  • (1/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (2020)Java后端开发----(面试题和笔试题)
  • (二)【Jmeter】专栏实战项目靶场drupal部署
  • (一)Java算法:二分查找
  • (一)pytest自动化测试框架之生成测试报告(mac系统)
  • (转)ABI是什么
  • .[hudsonL@cock.li].mkp勒索病毒数据怎么处理|数据解密恢复
  • .bat批处理(六):替换字符串中匹配的子串