2014-09-01 5 views
1

티타늄에 관해서는 작은 질문이 있습니다. 내가 뭘하려는 건 내 함수의 텍스트를 내보기의 레이블로 전달하는 것입니다. 누군가가 이것을 어떻게 성취 할 수 있는지 설명 할 수 있습니까? 이 함수는 다른 파일에 다음 레이블입니다.함수에서 티타늄으로보기 위해 텍스트를 보냅니다.

임이 일을하려고 : - 함수의 텍스트를 라벨에게>

를이 내 코드입니다 : 뷰의 코드 위의

//making the milking view 
var milkingView = Ti.UI.createView({ 
    backgroundColor: 'white' 
}); 

var title = new Ti.UI.createLabel({ 
    top: 20, 
    left: per25, 
    height: 30, 
    text: 'MilkingResults:', 
    color: 'black', 
    font: {fontSize: 18} 
}); 

var text = new Ti.UI.createLabel({ 
    top: 20, 
    left: per25, 
    height: 30, 
    text: 'here i want the text from the function', 
    color: 'black', 
    font: {fontSize: 18} 
}); 

되고, 아래로 아래의 코드입니다 사전 기능은

function http_get(subpage) { 
    var url = "url" + subpage; 
var http_client = Ti.Network.createHTTPClient({ 
    // function called when the response data is available 
    onload : function(e) { 
     Ti.API.info("Received text: " + this.responseText); 
     alert('success'); 
    }, 
    // function called when an error occurs, including a timeout 
    onerror : function(e) { 
     Ti.API.debug(e.error); 
     alert('error'); 
    }, 
    timeout : 5000 // in milliseconds 
}); 
// Prepare the connection. 
http_client.open("GET", url); 
// Send the request. 
http_client.send(); 

}; 

감사

답변

1

도전은 그쪽입니다 t http_client.onload 함수는 비동기입니다. 당신이 할 수있는 한 가지는 당신의 http_get 함수에서 콜백을 전달하는 것입니다. 당신의 http_get 그런 식으로

function updateLabel(textFromOnLoad){ 
    text.text = textFromOnLoad; 
}; 

수정 :

function http_get(subpage, callback) { 
    var url = "url" + subpage; 
var http_client = Ti.Network.createHTTPClient({ 
    // function called when the response data is available 
    onload : function(e) { 
     callback(this.responseText); 
    }, 
    // function called when an error occurs, including a timeout 
    onerror : function(e) { 
     Ti.API.debug(e.error); 
     alert('error'); 
    }, 
    timeout : 5000 // in milliseconds 
}); 
// Prepare the connection. 
http_client.open("GET", url); 
// Send the request. 
http_client.send(); 

}; 

그런 다음 첫 번째 파일에서 호출 할 수 있습니다 :

예를 들어, 첫 번째 파일에 다음과 같은 기능을 할 수 있습니다

http_get(subpage,updateLabel); 
+0

도움을 주셔서 감사합니다. 미안하지만, 나는 너무 늦게 awnser로 표시했습니다. –

관련 문제