Search code examples
javascriptappcelerator-alloyappcelerator-studio

How can get element generated by code in Appcelerator application


I'm building an app with appcelerator studio. Now I want to insert by code some element in my View.

So I'm building this code:

 var view1 = Ti.UI.createView({
          left : 0,
          width : "35%",
      top: "30px"
      });

    var label1 = createHeader(Titanium.Locale.getString(lang+"social_history")); 

    view1.add(label1);

    function createHeader(headerText){
        var heading = Ti.UI.createView({
        backgroundColor : "#0c7b84"
        });

        var headingText = $.UI.create("Label", {
        classes: 'headerTableLabel'
        });
        headingText.text = headerText;

        heading.add(headingText);

        return heading;
    }
....
....

This code works, but now I want to change throught an event a text of Label1. So I'm building this code that NOT works:

function set_fields(lang) {
    label1.text = Titanium.Locale.getString(lang + "social_history");
}

How can I get the element of the view, generated by code?


Solution

  • When adding label1 to view1, label1 becomes a child of view1. To get the children of a view, simply use view.getChildren() (returns an array of children) or var someChild = view.children[number_of_child_in_array] to get a certain child.

    In you case, var theLabel = view1.children[0] should give you label1

    Appcelerator Documentation - getChildren