Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Create Dynamic Variable using Loop

Hello,

What I am trying here to do is to create variable for views using for loop so I can use it in ScrollableView.

for e.g. here is what I did with no luck

var c=0;
for (c=0;c==3;c++)

{
    var view +c = Ti.UI.createView({
    backgroundColor:'red'
    });

}

I want the loop to create variable such as

view0
view1
view2

Can anyone help me in this??

— asked May 24th 2010 by Altaf Sayani
  • dynamic
  • javascript
  • loop
  • scrollableview
0 Comments

5 Answers

  • Accepted Answer

    change the "while" loop to a "for" one, and the "view - new Array();" to "var view=[];"
    here is a code that works

    http://pastie.org/974874

    — answered May 24th 2010 by Dan Tamas
    permalink
    0 Comments
  • why don't you use an array?

    
    var your_arr = [];
    
    for (c=0;c==3;c++)
    
    {
        your_arr[c] = Ti.UI.createView({
        backgroundColor:'red'
        });
    
    }
    
    — answered May 24th 2010 by Dan Tamas
    permalink
    0 Comments
  • I tried using array but then the views doesn't show up in ScrollableView.

    — answered May 24th 2010 by Altaf Sayani
    permalink
    0 Comments
  • Maybe you are making some mistake on the variable scope, and the function that tries to find the array is not finding it. Post more code pastie maybe we can help you :)

    — answered May 24th 2010 by Dan Tamas
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.