pass value to function

You must Login before you can answer or comment on any questions.

Does anyone know how can I pass the custom message to sayhi function ?

//win.js

var btn = new ui.button(standandBtn('Button'), sayhi)

function standandBtn(title){ var obj = {top:10, left:10, title:title}; return obj; }

function sayhi(){ trace('hello, how are you?'); }

// ui.js exports.button = function(args, fn){ var instance = Ti.UI.createButton(args); instance.addEventListener('click',fn); return instance; }

— asked 11 months ago by Louis Wong
1 Comment
  • //win.js
    var btn = new ui.button(standandBtn('Button'), sayhi);
    function standandBtn(title){ 
        var obj = {top:10, left:10, title:title}; 
        return obj;
     }
    function sayhi(){
     trace('hello, how are you?'); 
    }
     
    // ui.js
    exports.button = function(args, fn){ 
        var instance = Ti.UI.createButton(args);
        instance.addEventListener('click',fn);
        return instance; 
    }

    — commented 11 months ago by Louis Wong

1 Answer

Accepted Answer

You'll need to pass the data via the event object that is passed to each event listener.

You could add properties to the button itself, and then access those properties via the event's source property.

var btn = new ui.button(standandBtn('Button'), sayhi);
btn.somedata = 'foo'
 
function sayhi (e) {
    var btn = e.source;  // btn is now a reference to the originating button
    alert btn.somedata;
}

Your Answer

Think you can help? Login to answer this question!