GithubHelp home page GithubHelp logo

jspatch-demo's Introduction

JSPatch_Demo

####通过本例带领大家看看JSPatch的热修复,热更新的能力(她的应用场景太多了,我就不多说了)

  • 1.本例中我们要做的事情:修改按钮的背景颜色,高亮是图片

  • 2.修改按钮的点击事件

  • 3.JS新增类,创建JPTableViewController,实现数据源代理方法

  • 4.三方框架在JS中如何使用

  • 5.具体JSPatch请参考官方教程

  • ####先看一个图,这是没有修复前的效果图,点击按钮无法进去界面

    • 按钮本来输出"进不去界面"的
    • 但是通过JSPatch替换了这个方法
    • 直接查看index.js文件

image

  • ######按钮点击的事件的源码
- (void)buttonAction:(UIButton *)sender {
    NSLog(@"进不去界面");
}
  • ####修复后的效果图

image image

###index.js文件源码

/** 首页按钮点击方法替换 修改按钮背景颜色*/
defineClass('ViewController', {
            
            viewDidLoad:function() {
            self.ORIGviewDidLoad();
            var label = require('UILabel').alloc().initWithFrame({x:70, y:170, width:200, height:150});
            label.setText("我修改了一个巨大的错误,我能够hotfix,牛逼吧!");
            label.setNumberOfLines(0);
            label.setTextColor(require('UIColor').redColor());
            label.setBackgroundColor(require('UIColor').orangeColor());
            self.view().addSubview(label);
            
            label.mas__makeConstraints(block('MASConstraintMaker*', function(make) {
                                             
                                             make.leading().equalTo()(self.view()).offset()(40);
                                             make.trailing().equalTo()(self.view()).offset()(-40);
                                             make.top().equalTo()(self.view()).offset()(350);
                                             //                                            make.height().equalTo()(self.view()).multipliedBy()(0.3);
                                             make.height().mas__equalTo()(100);
                                             }));
            },
            
            setbtnBackgroudColor:function() {
            var btn = self.btn()
            var blueColor = require('UIColor').blueColor()
            btn.setBackgroundColor(blueColor)
            btn.setImage_forState(require('UIImage').imageNamed("znb 2"),1)

            },
            buttonAction: function(sender) {
            var tab = JPViewController.alloc().init()
            self.navigationController().pushViewController_animated(tab, YES)
        }
})


/** 更改,替换JPViewController页面的tabelview的点击方法*/
defineClass("JPViewController" , {
            tableView_didSelectRowAtIndexPath: function(tableView, indexPath) {
            var tab = JPTableViewController.alloc().init()
            self.navigationController().pushViewController_animated(tab, YES)
            }
})

/** tableviewController*/
defineClass("JPTableViewController : UITableViewController <UIAlertViewDelegate>", {
 dataSource: function() {
   var data = self.getProp('data')
   if (data) return data;
   var data = [];
   for (var i = 0; i < 20; i ++) {
            data.push("cell from js " + i);
        }
            self.setProp_forKey(data, 'data')
            return data;
        },
            numberOfSectionsInTableView: function(tableView) {
            return 1;
        },
            tableView_numberOfRowsInSection: function(tableView, section) {
            return self.dataSource().count();
        },
            tableView_cellForRowAtIndexPath: function(tableView, indexPath) {
            var cell = tableView.dequeueReusableCellWithIdentifier("cell")
            if (!cell) {
            cell = require('UITableViewCell').alloc().initWithStyle_reuseIdentifier(0, "cell")
        }
            cell.textLabel().setText(self.dataSource().objectAtIndex(indexPath.row()))
            return cell
        },
            tableView_heightForRowAtIndexPath: function(tableView, indexPath) {
            return 50
        },
            
            tableView_didSelectRowAtIndexPath: function(tableView, indexPath) {
            //返回上一页
//            self.navigationController().popViewControllerAnimated(YES)
            //返回首页
//            self.navigationController().popToRootViewControllerAnimated(YES)
            
            var alerView = require('UIAlertView').alloc().initWithTitle_message_delegate_cancelButtonTitle_otherButtonTitles(
                                                                                                                             "Alert",
                                                                                                                             self.dataSource().objectAtIndex(indexPath.row()), 
                                                                                                                             self, 
                                                                                                                             "OK",
                                                                  null
                                                                                                                             )
            alerView.show()
            },
            
            alertView_clickedButtonAtIndex: function(alertView, buttonIndex) {
            console.log('clicked index ' + buttonIndex)
            }
          
})













jspatch-demo's People

Contributors

znbmm avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.