Titanium 如何在当前窗口中更新文本标签

Titanium 如何在当前窗口中更新文本标签,titanium,appcelerator,titanium-mobile,appcelerator-mobile,Titanium,Appcelerator,Titanium Mobile,Appcelerator Mobile,我需要用JSON响应返回的新数据更新win1.title(标签)。 我可以在我的控制台上打印win1.title值,但我无法为其分配新值 app.js var win1 = Titanium.UI.createWindow({ title:'Tab 1', backgroundColor: 'black', layout: 'vertical', url: 'win1.js', title: 'Loading...', artist: '' });

我需要用JSON响应返回的新数据更新win1.title(标签)。 我可以在我的控制台上打印win1.title值,但我无法为其分配新值

app.js

var win1 = Titanium.UI.createWindow({
    title:'Tab 1',
    backgroundColor: 'black',
    layout: 'vertical',
    url: 'win1.js',
    title: 'Loading...',
    artist: '' });


win1.open();

//Fetching data

var jsonData = ''; var pointer = 0;

var url = "http://example.com"; var xhr = Ti.Network.createHTTPClient({
    onload: function(e) {

        jsonData = JSON.parse(this.responseText).response.songs;



        //HERE I NEED TO UPDATE win1.title with title returned by JSON
        /*
           if a print win1.title it works correctly.
             console.log(win1.title);

           but if I try to assign data to win1.title nothing happens, even a error!

        */


        win1.addEventListener('swipe', function(e) {
                        console.log("win1 title:" + win1.title);            win1.title = jsonData[pointer].title;           win1.artist = jsonData[pointer].artist_name;            win1.image = jsonData[pointer].tracks[0].release_image;

                    });

    },
    onerror: function(e) {
        console.log(e);
    },
    timeout:20000  /* in milliseconds */ }); xhr.open("GET", url); xhr.send();  // request is actually sent with this statement
(function() {

    var win1 = Ti.UI.currentWindow;

    var image = Ti.UI.createImageView({
      image:win1.image,
      top: 40
    });

    var title = Ti.UI.createLabel({
      color: 'white',
      font: { fontSize:38 },
      text: win1.title,
      textAlign: Ti.UI.TEXT_ALIGNMENT_CENTER,
      top: 20,
      width: 'auto', height: 'auto'
    });

    var artist = Ti.UI.createLabel({
      color: 'white',
      font: { fontSize:28 },
      text: win1.artist,
      textAlign: Ti.UI.TEXT_ALIGNMENT_CENTER,
      top: 30,
      width: 'auto', height: 'auto'
    });


    win1.add(title);
    win1.add(artist);
    win1.add(image);


})();
win1.js

var win1 = Titanium.UI.createWindow({
    title:'Tab 1',
    backgroundColor: 'black',
    layout: 'vertical',
    url: 'win1.js',
    title: 'Loading...',
    artist: '' });


win1.open();

//Fetching data

var jsonData = ''; var pointer = 0;

var url = "http://example.com"; var xhr = Ti.Network.createHTTPClient({
    onload: function(e) {

        jsonData = JSON.parse(this.responseText).response.songs;



        //HERE I NEED TO UPDATE win1.title with title returned by JSON
        /*
           if a print win1.title it works correctly.
             console.log(win1.title);

           but if I try to assign data to win1.title nothing happens, even a error!

        */


        win1.addEventListener('swipe', function(e) {
                        console.log("win1 title:" + win1.title);            win1.title = jsonData[pointer].title;           win1.artist = jsonData[pointer].artist_name;            win1.image = jsonData[pointer].tracks[0].release_image;

                    });

    },
    onerror: function(e) {
        console.log(e);
    },
    timeout:20000  /* in milliseconds */ }); xhr.open("GET", url); xhr.send();  // request is actually sent with this statement
(function() {

    var win1 = Ti.UI.currentWindow;

    var image = Ti.UI.createImageView({
      image:win1.image,
      top: 40
    });

    var title = Ti.UI.createLabel({
      color: 'white',
      font: { fontSize:38 },
      text: win1.title,
      textAlign: Ti.UI.TEXT_ALIGNMENT_CENTER,
      top: 20,
      width: 'auto', height: 'auto'
    });

    var artist = Ti.UI.createLabel({
      color: 'white',
      font: { fontSize:28 },
      text: win1.artist,
      textAlign: Ti.UI.TEXT_ALIGNMENT_CENTER,
      top: 30,
      width: 'auto', height: 'auto'
    });


    win1.add(title);
    win1.add(artist);
    win1.add(image);


})();

设置
win1.title='some title'不会执行您认为会执行的操作。有一个属性,并且取决于您是为iOS还是Android构建,您将在模式窗口的顶部或窗口位于选项卡组或导航组中时看到此标题

您的代码正在更新此标题,但您可能没有看到它。(尝试将
modal:true
添加到
createWindow()
声明中。)此外,您已经设置了2个
title
属性,因此请删除其中一个属性

要更改win1.js上名为“title”的变量中的标签文本,可以执行以下操作:

win1.updateTitle = function(newTitle){
    title.text = newTitle;
} 
在win1.js中,添加以下内容:

win1.updateTitle = function(newTitle){
    title.text = newTitle;
} 
然后,回到app.js,转到您想要更新标题的任何地方,并执行以下操作:

win1.updateTitle('new title');

此外,您应该考虑使用与您的钛项目相同的Jujs:


设置
win1.title='some title'不会执行您认为会执行的操作。有一个属性,并且取决于您是为iOS还是Android构建,您将在模式窗口的顶部或窗口位于选项卡组或导航组中时看到此标题

您的代码正在更新此标题,但您可能没有看到它。(尝试将
modal:true
添加到
createWindow()
声明中。)此外,您已经设置了2个
title
属性,因此请删除其中一个属性

要更改win1.js上名为“title”的变量中的标签文本,可以执行以下操作:

win1.updateTitle = function(newTitle){
    title.text = newTitle;
} 
在win1.js中,添加以下内容:

win1.updateTitle = function(newTitle){
    title.text = newTitle;
} 
然后,回到app.js,转到您想要更新标题的任何地方,并执行以下操作:

win1.updateTitle('new title');

此外,您应该考虑使用与您的钛项目相同的Jujs:


up投票赞成强调CommonJS的重要性(因此是好的架构)up投票赞成强调CommonJS的重要性(因此是好的架构)