2012-02-20 9 views
0

私はMobile App開発用のJavaScriptを使用してJavaScriptで作業しています。私は、bn int countを使ってforループで問題が発生しています。ボタン[bn] .setTitle( '*')は、イベントハンドラで作成されたすべてのボタンで常に5になります。または、forループの外にある// bn = 0のコメントを外すと、値ゼロ。JavaScript Titaniumアプリケーションの割り当て

私の考えでは、作成時には各ボタン、イベントハンドラなどに値を割り当て、カウントを前進させるときに戻ったり変更したりしないでください。私はここで何が欠けているのか、あるいは違うことをする必要がありますか

/** 
* create a view object to hold the buttons 
* and add the buttons into the view 
*/ 
function createRatingButtons(numButtons,BarTitle,topspace) { 
    // set vars 
    var bn=0; 
    var left = 5; 
    var top = 5; 
    /* 
    * create a view for the buttons 
    */ 
    var ratingView = Titanium.UI.createView({ 
     height:  100, 
     color:  'white', 
     top:  topspace, 
    }); 

    /* 
    * create a label to put into the view 
    */ 
    var ratingLabel = Titanium.UI.createLabel({ 
       text:       BarTitle, 
       color:      '#fff', 
       backgroundColor: 'transparent', 
       textAlign:    'left', 
       height:      'auto', 
       width:      'auto', 
       top:       0, 
       left:       left,  
    }) 
    ratingView.add(ratingLabel); 
    /* 
    * do the for loop and add 
    * the buttons to the view 
    */ 
     var button = []; 
    for(bn==0;bn<numButtons;bn++) { 
        button[bn] = Titanium.UI.createButton({ 
       title:    bn, 
       width:    50, 
       height:    50, 
       color:    "black", 
       // backgroundColor: "blue", 
       left:    left, 
       top:        top+ratingLabel.getHeight(), 
      }); 
      /* 
      * Add event handler for this button 
      */ 
      button[bn].addEventListener('click', function(e) 
         { 
          Ti.API.info("Rating Button Click #: " + bn); 
          /* 
          * Update buttons below this count for this object 
          * to have colored stars, and all starts after this 
          * to be uncolored. 
          */ 
          button[bn].setTitle('*') 
         }); 


      ratingView.add(button[bn]); 
      left = left + 50 + 5; 
    } 
    //bn = 0; 

    // return the entire block for this view 
    return ratingView; 
} 
+0

'scrollView.add(createRatingButtons(5、" test1 "、0)); scrollView.add(createRatingButtons(5、 "test2"、95)); ' – user1221705

答えて

0

これを試してください:あなたは匿名関数の内部bnをアクセスしようとしている

function createRatingButtons(numButtons, BarTitle, topspace) { 
    // set vars 
    var bn = 0; 
    var left = 5; 
    var top = 5; 

    // create a view for the buttons 
    var ratingView = Titanium.UI.createView({ 
     height: 100, 
     color: 'white', 
     top: topspace, 
    }); 

    // create a label to put into the view 
    var ratingLabel = Titanium.UI.createLabel({ 
     text:   BarTitle, 
     color:   '#fff', 
     backgroundColor: 'transparent', 
     textAlign:  'left', 
     height:   'auto', 
     width:   'auto', 
     top:    0, 
     left:   20 
    }); 
    ratingView.add(ratingLabel); 

    // add the buttons to the view 
    var button = []; 

    for(bn = 0; bn < numButtons; bn++) { 
     button[bn] = Titanium.UI.createButton({ 
      title: bn, 
      width: 50, 
      height: 50, 
      color: "black", 
      left: left, 
      top: top+ratingLabel.getHeight() 
     }); 

     // add event handler for buttons 
     button[bn].addEventListener('click', function(e) { 
      Ti.API.info("Rating Button Click #: " + e.source.title); 

      /* 
      * Update buttons below this count for this object 
      * to have colored stars, and all starts after this 
      * to be uncolored. 
      */ 
      e.source.setTitle('*'); 
     }); 

     ratingView.add(button[bn]); 
     left = left + 50 + 5; 
    } 

    // return the entire block for this view 
    return ratingView; 
} 


scrollView.add(createRatingButtons(5, "test1", 0)); 
scrollView.add(createRatingButtons(5, "test2", 95)); 
0
  1. 。匿名関数が実行されると、bnの値 が評価されます。 でbnの値は1より小さく、 numButtonsの値より小さくなります。

    匿名関数内でbnを使用しないでください。 e.sourceを使用して、ボタンへの参照を取得できます。

  2. も、私はあなたがループのためにあなたに

==

を使用して誤っていると思います。

for(bn==0;bn<numButtons;bn++)