2012-09-17 94 views
0

我從服務器獲取JSON字符串:設置選項,選項對話框鈦

genre [ 
'type1', 
'type2', 
'type3', 
'type4' 

]

我想在數組類型值添加到option_dialog.I選項宣佈:

var genreArr=[]; 
for(var i=0;i<genre.length;i++) 
{ 
genreArr.push(genre[i])); 
} 

然後我創建選項對話框併爲其設置選項。

var option_dialog=Ti.UI.createOptionDialog({}); 
option_dialog.options=genreArr; 

但是,當點擊選項對話框它陣列不」顯示值genreArr.Can你幫me.Thank

+0

哪裏是你的事件處理程序?它執行 'option_dialog.show()' 和你的目標設備是什麼? 似乎有一個額外的')'在 'genreArr.push(genre [i]));' – C5H8NNaO4

回答

2

options是一個創造只讀屬性,如the documentation規定。您必須將它傳遞給您的createOptionDialog調用,如下所示。

var genre = [ 
    'type1', 
    'type2', 
    'type3', 
    'type4' 
]; 

var dialog = Ti.UI.createOptionDialog({ 
    options: genre, 
    title: 'Pick a Genre' 
}); 
dialog.addEventListener('click', function(evt) 
{ 
    alert('You picked ' + genre[evt.index]); 
}); 
dialog.show(); 
+0

你可以試着向我解釋爲什麼這個代碼適合我在iOS? : 'VAR贏= Ti.UI.createWindow({}) \t win.open() \t \t VAR showopt = Ti.UI.createButton({ \t \t標題: 「顯示選項」 \t}) \t showopt.addEventListener( 「點擊」,函數(){ \t \t opt.show(); \t}) \t win.add(showopt) \t optarr = [ 「測試」, 「測試1」,「的Test2 「] \t va ř選擇= Ti.UI.createOptionDialog({ \t \t //選項:optarr \t}) \t opt.options = optarr' 感謝=) – C5H8NNaO4

+0

規格從發散。 –

+0

感謝您的回答.. – user1060362

0

您可以通過這種方式,我有我的代碼中使用使用

var genre = [ 
    'type1', 
    'type2', 
    'type3', 
    'type4' 
]; 

//調用選項對話框中的onclick聽衆

var dialog = Ti.UI.createOptionDialog(genre).show(); 
0
var win = Titanium.UI.createWindow({ 
    title:"Prompting the Device to Vibrate", 
    backgroundColor:"#FFFFFF" 
}); 

var label = Titanium.UI.createLabel({ 
    text:"Your choice will appear here", 
    width:"auto", 
    height:"auto" 
}); 

//The option dialog is very similar to the alert dialog in its creation and handling 
var colorDialog = Titanium.UI.createOptionDialog({ 
    title:"What color do you like?", 
    options: ["Yellow","Surprise me","No thanks"], 
    cancel:2//The index in the options array that defines which of the buttons is a cancel button 
}); 

var optionButton = Titanium.UI.createButton({ 
    title:"Option Dialog!", 
    height:48, 
    width:120, 
    bottom:12 
}); 

colorDialog.addEventListener("click",function(e){ 
    if(e.index < 2){ 
     if(e.index === 0){ 
      win.backgroundColor = "#FFCC00"; 
     }else if(e.index === 1){ 
      win.backgroundColor = "#329FF0"; 
     } 

    //Access the button index via e.index 
    //Use that index with e.source.buttonNames to return th button name tapped: e.source.buttonNames[e.index] 
    label.text = "You tapped index " + e.index; 

    }else{ 
     label.text = "Aww shucks..."; 
    } 

}); 

optionButton.addEventListener("click",function(e){ 
    colorDialog.show(); 
}); 


win.add(label); 
win.add(optionButton); 
win.open();