2014-11-05 72 views
0

我使用鈦合金MVC與需要掃描QR碼 鈦SDK 3.4.0.GA返回結果從另一個控制器鈦合金

項目控制器我有2個控制器:index.js和secondwindow。 js和它們各自的視圖index.xml和secondwindow.xml。 我要開始掃描並處理secondWindow控制器掃描的結果,而結果返回到索引控制器,讓索引處理自己的UI元素

我試着像這樣 INDEX.XML:

<Alloy> 
    <Window> 
    <Label id='result' /> 
    ...Other components... 
    <Button onClick='startScan'>Start QR scan</Button> 
    </Window> 
</Alloy> 

index.js:

function whenSecondWindowFinish(arg){ 
    //update index.xml 
    $.result.setText(arg); 
} 

function startScan(e){ 
    Alloy.createController('secondWindow'); 
} 

$.index.open(); 



secondWindow.xml:

<Alloy> 
    <Window exitOnClose='false'> 
    </Window> 
</Alloy> 

secondWindow.js:

function scanOK(data){ 
var returnResult = /*Handle data*/ 
//I need to return the result to the index controller 
$.secondWindow.close();//And close this view 
} 

function canceled(){ 
//return {} to index controller 
$.secondWindow.close();//And close this view 
} 

var QRscanner = require('qrscanner'); 
var qroptions = { 
    //width height ... 
    success: scanOK, 
    cancel: canceled 
}; 
var qrview = QRscanner.createQRView(qroptions); 
$.secondWindow.add(qrview); 
$.secondWindow.open(); 


我怎樣才能成功關閉此窗口/取消功能,並將結果返回到索引控制器或通知索引執行whenSecondWindowFinish掃描(/ 通ARG結果 /);方法?或者哪種方法是正確的?

+0

我瞭解的是你想從'secondWindow.js'發送一些結果'index.js'(對嗎?)和你想發送的結果是什麼類型(String,object,other)? – turtle 2014-11-06 03:32:15

回答

1

使用回調。

index.js:

var callbackFunc = function(data){ 
    //do something with the data variable 
} 
Alloy.createController('secondwindow', {'callback':callbackFunc}); 

secondwindow.js:

var args = arguments[0] || {}; 

function scanOK(data){ 
    args.callback(data) 
//I need to return the result to the index controller 
$.secondWindow.close();//And close this view 
} 

你也可以使用一個Ti.App.fireEvent得到同樣的事情,但在這裏就是爲什麼你不應該:http://www.tidev.io/2014/09/10/the-case-against-ti-app-fireevent-2/ (哦,該鏈接也解釋了回調:)

0

使用這一個: -

activity.startActivityForResult(intent, function(e) { 
    // The request code used to start this Activity 
    var requestCode = e.requestCode; 
    // The result code returned from the activity 
    // (http://developer.android.com/reference/android/app/Activity.html#StartingActivities) 
    var resultCode = e.resultCode; 
    // A Titanium.Android.Intent filled with data returned from the Activity 
    var intent = e.intent; 
    // The Activity the received the result 
    var source = e.source; 
}); 

OR

Have a look here

斯巴達克斯謝謝:)

+0

謝謝,但我需要的代碼與iOS和Android一起工作,您的代碼僅適用於Android,我想我會使用回調作爲參數方法 – RPAZ 2014-11-11 16:27:15